魔法方法__autoload失效的解决办法spl_autoload_register()

本文探讨了在使用Smarty模板引擎3.1.8版本时遇到的PHP自动加载问题。由于Smarty自身注册了autoload函数并使用spl_autoload_register(), 导致自定义的__autoload()方法失效。文章解释了如何正确地注册多个自动加载函数。

今天用PHP写程序,定义了自动加载的魔法方法__autolaod()之后却不起作用。google之后才发现,我用了smarty模板引擎3.1.8版本,而在该版本中smarty定义了自己的autoload函数,然后用spl_autoload_register() 进行注册。再查看spl_autoload_register() 的定义我们会发现:

If your code has an existing __autoload function then this function must be explicitly registered on the __autoload stack. This is because spl_autoload_register() will effectively replace the engine cache for the __autoload function by either spl_autoload() or spl_autoload_call().

If there must be multiple autoload functions, spl_autoload_register() allows for this. It effectively creates a queue of autoload functions, and runs through each of them in the order they are defined. By contrast, __autoload() may only be defined once.


也就是说,如果你有多个autoload函数的话,最好使用spl_autoload_register() 进行注册,而spl_autoload_register() 的使用会导致__autoload()的失效。

评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值