Example of php auto-loading mechanism. 1. the code for copying the custom function 2 and spl_autoload_register () is as follows: liuyuan @ ebuinfo: varwwwphpgcsphp_autoload $ ll. *-rw-r -- 1liuyuanliuyuan800Feb1911: 39. func 1, user-defined function
2, spl_autoload_register ()
The code is as follows:
Liuyuan @ ebuinfo:/var/www/phpgcs/php_autoload $ ll ./*
-Rw-r -- 1 liuyuan 800 Feb 19 :39./func_autoload.php
-Rw-r -- 1 liuyuan 906 Feb 19 :28./spl_autoload.php
./Include:
Total 16
Drwxrwxr-x 2 liuyuan 4096 Feb 19 11: 42 ./
Drwxrwxr-x 3 liuyuan 4096 Feb 19 11: 43 ../
-Rw-r -- 1 liuyuan 142 Feb 19 :42 aClass. php
-Rw-r -- 1 liuyuan 143 Feb 19 :42 bClass. php
First, let's look at the custom function method:
The code is 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 running result is as follows:
The code is 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
)
Method 2:
The code is 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 ('myload', 'autoload '));
/**
* The _ autoload method will expire after spl_autoload_register, because the autoload_func function pointer has pointed to the spl_autoload method
* You can add the _ autoload method to the autoload_functions list using 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 running result is as follows:
The code is 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
Example 2, spl_autoload_register () Code: liuyuan @ ebuinfo:/var/www/phpgcs/php_autoload $ ll. /*-rw-r -- 1 liuyuan 800 Feb 19 11: 39. /func...