PHP automatic loading AutoLoad mechanism sample sharing _php Tutorial

Source: Internet
Author: User
Tags autoload eol
1, Custom function

2,spl_autoload_register ()

Copy the Code code as follows:
liuyuan@ebuinfo:/var/www/phpgcs/php_autoload$ LL./*
-rw-rw-r--1 Liuyuan Liuyuan 11:39./func_autoload.php
-rw-rw-r--1 Liuyuan Liuyuan 906 Feb 11:28./spl_autoload.php

./include:
Total 16
Drwxrwxr-x 2 Liuyuan Liuyuan 4096 Feb 19 11:42.
Drwxrwxr-x 3 Liuyuan Liuyuan 4096 Feb 19 11:43.. /
-rw-rw-r--1 Liuyuan Liuyuan 142 Feb 11:42 aclass.php
-rw-rw-r--1 Liuyuan Liuyuan 143 Feb 11:42 bclass.php

First look at the Custom Function mode:

Copy the Code code as follows:
Define (' EOL ', (Php_sapi = = ' cli ')? Php_eol: " );
Print_r (Get_included_files ());
Echo EOL;
Print Get_include_path ();
Echo EOL;
Set_include_path (Get_include_path (). Path_separator. ' /var/www/ly_php/php_spl/include/');
Set_include_path (DirName (__file__). ' /include ');
Set_include_path (DirName (__file__). ' /include/');

function __autoload ($className) {
$filename = './include/'. $className. PHP ';
$filename = './include/'. $className. PHP ';
$filename = '/var/www/ly_php/php_spl/include/'. $className. PHP ';
if (file_exists ($filename)) {
Include_once $filename;
}else{
Exit (' no file ');
}
}

$a = new AClass ();
$b = new Bclass ();
Print_r (Get_included_files ());
?>

The results of the operation are as follows:

Copy the Code code as follows:
liuyuan@ebuinfo:/var/www/phpgcs/php_autoload$ PHP func_autoload.php
Array
(
[0] =/var/www/phpgcs/php_autoload/func_autoload.php
)

.:/ Usr/share/php:/usr/share/pear
AClass is loaded
Bclass is loaded
Array
(
[0] =/var/www/phpgcs/php_autoload/func_autoload.php
[1] =/var/www/phpgcs/php_autoload/include/aclass.php
[2] =/var/www/phpgcs/php_autoload/include/bclass.php
)

The second way:

Copy the Code code as follows:
Class myloader{
public static function AutoLoad ($className) {
$filename = './include/'. $className. PHP ';
if (file_exists ($filename)) {
Include_once $filename;
}else{
Exit (' no file ');
}
}
}

Define (' EOL ', (Php_sapi = = ' cli ')? Php_eol: '
');

Spl_autoload_register (Array (' Myloader ', ' autoload '));

/**
The *__autoload method is invalidated after spl_autoload_register because the Autoload_func function pointer is pointing to the Spl_autoload method
* The _autoload method can be added to the Autoload_functions list by the following method
*/
Spl_autoload_register (' __autoload ');


Error_reporting (E_all^e_notice^e_warning^e_error);
Error_reporting (E_notice | e_warning);

$a = new AClass ();
Print_r (Get_included_files ());
Echo EOL;
$b = new Bclass ();
Echo EOL;
?>

The results of the operation are as follows:

Copy the Code code as follows:
liuyuan@ebuinfo:/var/www/phpgcs/php_autoload$ PHP spl_autoload.php
AClass is loaded
Array
(
[0] =/var/www/phpgcs/php_autoload/spl_autoload.php
[1] =/var/www/phpgcs/php_autoload/include/aclass.php
)

Bclass is loaded

http://www.bkjia.com/PHPjc/825273.html www.bkjia.com true http://www.bkjia.com/PHPjc/825273.html techarticle 1, Custom Function 2,spl_autoload_register () Copy the Code code as follows: liuyuan@ebuinfo:/var/www/phpgcs/php_autoload$ LL./*-rw-rw-r--1 Liuyuan Liuyuan 11:39./func ...

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.