<?php //spl_autoload_register:多次注册,队列执行 spl_autoload_register(function($className) { $filePath='./'.str_replace('\\','/',$className).'.php'; if(!file_exists($filePath)){ throw new Exception('file not found.'); } require_once $filePath; }); $a=new \Demo\A(); $a->eat(); ?>
./demo/A.php文件:
<?php
namespace demo;
class A{
public function eat()
{
echo '吃';
}
}
?>