PHP 类的自动载入有两种方法,__autoload() 和 spl_autoload_register() ,就是在PHP代码中new一个类的时候,会自动触发,将类的类名包括命名空间作为参数传进入方法里,在方法里可根据命名空间和类名准确找到类文件,从而require或者inlcude进来。菜鸟一枚,作为备忘
<?phpfunction 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"
PHP 类的命名空间 和自动载入
原文地址:https://www.cnblogs.com/undefined-j/p/10399374.html