PHP 類的自動載入有兩種方法,__autoload() 和 spl_autoload_register() ,就是在PHP代碼中new一個類的時候,會自動觸發,將類的類名包括命名空間作為參數傳進入方法里,在方法里可根據命名空間和類名准確找到類文件,從而require或者inlcude進來。菜鳥一枚,作為備忘
<?php function auto($class){ //$class = A\B\E; /** 命名空間的自動載入 **/ $class_path = explode("\\",$class); $file = __DIR__ . '/' ; foreach($class_path as $c){ $file .= $c . '/'; } $file = rtrim($file,"/"); $file .= '.php'; var_dump($file);exit; } spl_autoload_register('auto'); use A\B\E; $e = new E(); echo 'hi'; /*******輸出*******/ string(32) "/www/test_php_autoload/A/B/E.php"