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





