1, custom functions
2,spl_autoload_register ()
Copy Code code as follows:
liuyuan@ebuinfo:/var/www/phpgcs/php_autoload$ LL./*
-rw-rw-r--1 Liuyuan Liuyuan Feb 11:39./func_autoload.php
-rw-rw-r--1 Liuyuan Liuyuan 906 Feb. 11:28
./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 Feb 11:42 aclass.php
-rw-rw-r--1 Liuyuan Liuyuan 143 Feb 11:42
First look at the Custom Function mode:
Copy Code code as follows:
<?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 ());
?>
The results of the operation are as follows:
Copy 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 Code code as follows:
<?php
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: ' <br/> ');
Spl_autoload_register (Array (' Myloader ', ' autoload '));
/**
The *__autoload method fails 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 in the following ways
*/
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 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