首页 > 为什么要用spl_autoload_register来取代__autoload()

为什么要用spl_autoload_register来取代__autoload()

为什么要用spl_autoload_register来取代__autoload()
除了可以多次注册之外


http://www.jb51.net/article/37746.htm
这里说得很详细


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.

spl_autoload_register 可以很好地处理需要多个加载器的情况,这种情况下 spl_autoload_register 会按顺序依次调用之前注册过的加载器。作为对比, __autoload 因为是一个函数,所以只能被定义一次。

这是英文文档里的一句话,解释了加入 spl_autoload_register 的意义,但是好像在中文文档中没有。


spl_autoload_register
(PHP 5 >= 5.1.2)
spl_autoload_register — 注册__autoload()函数
说明
bool spl_autoload_register ([ callback $autoload_function ] )
将函数注册到SPL __autoload函数栈中。如果该栈中的函数尚未激活,则激活它们。
如果在你的程序中已经实现了__autoload函数,它必须显式注册到__autoload栈中。因为
spl_autoload_register()函数会将Zend Engine中的__autoload函数取代为spl_autoload()或
spl_autoload_call()。

autoload_function
注册的自动装载函数。如果没有提供任何参数,则自动注册autoload的默认实现函数
spl_autoload()。
说明
如果成功则返回 TRUE,失败则返回 FALSE。
注:SPL是Standard PHP Library(标准PHP库)的缩写。它是PHP5引入的一个扩展库,其主要功能包括autoload机制的实现及包括各种Iterator接口或类。SPL autoload机制的实现是通过将函数指针autoload_func指向自己实现的具有自动装载功能的函数来实现的。SPL有两个不同的函数spl_autoload, spl_autoload_call,通过将autoload_func指向这两个不同的函数地址来实现不同的自动加载机制!

【热门文章】
【热门文章】