spl_autoload_register

1.函数详解
bool spl_autoload_register ([ callable $autoload_function [, bool $throw = true [, bool $prepend = false ]]] )
如果需要多条 autoload 函数,spl_autoload_register() 满足了此类需求。 它实际上创建了 autoload 函数的队列,按定义时的顺序逐个执行。相比之下, __autoload() 只可以定义一次。

/*
参数
autoload_function
欲注册的自动装载函数。如果没有提供任何参数,则自动注册 autoload 的默认实现函数spl_autoload()。

throw
此参数设置了 autoload_function 无法成功注册时, spl_autoload_register()是否抛出异常。

prepend
如果是 true,spl_autoload_register() 会添加函数到队列之首,而不是队列尾部。*/
2.例子
//test1.php
namespace Test;
class test1{ public static function ceshi(){ echo __METHOD__; echo '</br>'; } }
//test2.php
namespace Test;
class test2{
    public static function ceshi(){
        echo __METHOD__; //返回类的名字和方法的名字
        echo '</br>';
    }
}
//index.php
spl_autoload_register('autoload_self');

Test\test1::ceshi();//调用静态方法格式-->命名空间名\类名::静态方法名

Test\Test2::ceshi();

function autoload_self($class){//定义引入文件函数
    //echo $class ===> Test\test1
    list($namespace,$fileName) = explode('\\',$class);
    require __DIR__.'\\'.$fileName.'.php';
}

输出

Test\test1::ceshi
Test\test2::ceshi

转载于:https://www.cnblogs.com/zxqblogrecord/p/9965045.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值