PHP 类的命名空间 和自动载入

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"

 

转载于:https://www.cnblogs.com/undefined-j/p/10399374.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值