1, custom function
2, spl_autoload_register()
Copy code The code is as follows:
liuyuan@ebuinfo:/var/www/phpgcs/php_autoload$ ll ./*
-rw-rw-r-- 1 liuyuan liuyuan 800 Feb 19 11:39 ./func_autoload.php
-rw-rw-r-- 1 liuyuan liuyuan 906 Feb 19 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 19 11:42 aClass.php
-rw-rw-r-- 1 liuyuan liuyuan 143 Feb 19 11:42 bClass.php
First look at the custom function method:
Copy code The code is as follows:
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 = ' if(file_exists($filename)){
include_once $filename;
}else{
exit('no file');
}
}
$a = new aClass();
$b = new bClass();
print_r(get_included_files());
?>
The running results are as follows:
Copy code 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
)
The second way:
Copy code 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('myLoader', 'autoload'));
/**
*The __autoload method will become invalid after spl_autoload_register, because the autoload_func function pointer already points to the spl_autoload method
* You can add the _autoload method 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;
?>
运行结果如下:
复制代码 代码如下:
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.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/825273.htmlTechArticle1,自定义函数 2,spl_autoload_register() 复制代码 代码如下: liuyuan@ebuinfo:/var/www/phpgcs/php_autoload$ ll ./* -rw-rw-r-- 1 liuyuan liuyuan 800 Feb 19 11:39 ./func...
Statement:The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn