The AutoLoad of PHP basics
Last Update:2017-02-28
Source: Internet
Author: User
PHP automatic loading autoload mechanism is very important, here do 2 small exercises original articles, reproduced please note: Http://www.cnblogs.com/phpgcs file structure as follows, 2 ways to implement automatic loading 1, custom functions 2,spl_autoload_register () liuyuan@ebuinfo:/var/www/phpgcs/php_autoload$ LL./*-rw-rw-r-- 1 Liuyuan Liuyuan 800 Feb 11:39./func_autoload.php-rw-rw-r--1 Liuyuan Liuyuan 906 Feb. 11:28 oload.php /include:total drwxrwxr-x 2 Liuyuan liuyuan 4096 Feb-11:42./drwxrwxr-x 3 Liuyuan Liuyuan 4096 F EB 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 BClas s.php First look at the Custom Function mode: <?php define (' EOL ', (php_sapi = ' cli ')? Php_eol: ' </br> '); 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 ())?> running results are as follows: + View Code The second way: &NBS P <?php class myloader{ public static function AutoLoad ($className) { &NB Sp $filename = './include/'. $className. PHP '; if (file_exists ($filename)) { &NB Sp Include_once $filename; }else{ exit (' no file '); { } define (' EOL ', (Php_sapi = = ' cli ')? Php_eol: ' <br/> '); Spl_autoload_register (Array (' Myloader ', ' autoload ')); /** *__autoload method will fail 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 / //spl_autoload_register in the following ways (' __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;?>