The autoload mechanism of PHP is very important. Here are 2 small exercises.
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 running results are as follows:
+ View Code
The second way:
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 through 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;
?>
http://www.bkjia.com/PHPjc/735880.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/735880.htmlTechArticlePHP’s autoload autoload mechanism is very important. Here are 2 small exercises. Original article, please indicate when reprinting: http ://www.cnblogs.com/phpgcs The file structure is as follows, 2 ways to achieve automatic loading 1,...
Statement of this Website
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