<?php
//自动加载函数
//__autoload函数只能全局的声明一次,整个php项目的运行中,它只能声明一次
function __autoload($className)
{
$path='./'.$className.'.php';
if(!file_exists($path)){
throw new Exception('file not found.');
}
require_once $path;
}
//spl_autoload_register:多次注册,队列执行
$a=new A();
$a->eat();
?>
<?php
//spl_autoload_register:多次注册,队列执行
spl_autoload_register(function($className)
{
$filePath='./'.$className.'.php';
if(!file_exists($filePath)){
throw new Exception('file not found.');
}
require_once $filePath;
});
$a=new A();
$a->eat();
?>
<?php
class A{
public function eat()
{
echo '吃';
}
}
?>