spl_autoload_register() - SPL函数
spl_autoload_register()
(PHP 5 >= 5.1.0, PHP 7)
注册给定的函数作为 __autoload 的实现
说明
spl_autoload_register([callable $autoload_function[,bool $throw= true[,bool $prepend= false]]]): bool将函数注册到SPL __autoload函数队列中。如果该队列中的函数尚未激活,则激活它们。
如果在你的程序中已经实现了__autoload()函数,它必须显式注册到__autoload()队列中。因为spl_autoload_register()函数会将Zend Engine中的__autoload()函数取代为spl_autoload()或spl_autoload_call()。
如果需要多条 autoload 函数,spl_autoload_register()满足了此类需求。它实际上创建了 autoload 函数的队列,按定义时的顺序逐个执行。相比之下,__autoload()只可以定义一次。
参数
$autoload_function欲注册的自动装载函数。如果没有提供任何参数,则自动注册 autoload 的默认实现函数spl_autoload()。
$throw此参数设置了$autoload_function无法成功注册时,spl_autoload_register()是否抛出异常。
$prepend如果是 true,spl_autoload_register()会添加函数到队列之首,而不是队列尾部。
返回值
成功时返回TRUE
,或者在失败时返回FALSE
。
更新日志
版本 | 说明 |
---|---|
5.3.0 | 引入了命名空间的支持。 |
5.3.0 | 添加了$prepend参数。 |
范例
spl_autoload_register()作为__autoload()函数的替代
class 未能加载的spl_autoload_register()例子
以上例程的输出类似于:
[[Foobar\InexistentClass]] Fatal error: Class 'Foobar\InexistentClass' not found in ...
参见
- __autoload() 尝试加载未定义的类
Good news for PHP 5.3 users with namespaced classes: When you create a subfolder structure matching the namespaces of the containing classes, you will never even have to define an autoloader. It is recommended to use only one extension for all classes. PHP (more exactly spl_autoload) does the rest for you and is even quicker than a semantically equal self-defined autoload function like this one: I compared them with the following setting: There are 10 folders, each having 10 subfolders, each having 10 subfolders, each containing 10 classes. To load and instantiate these 1000 classes (parameterless no-action constructor), the user-definded autoload function approach took 50ms longer in average than the spl_autoload function in a series of 10 command-line calls for each approach. I made this benchmark to ensure that I don't recommend something that could be called "nice, but slow" later. Best regards,
When switching from using __autoload() to using spl_autoload_register keep in mind that deserialization of the session can trigger class lookups. This works as expected: This will result in "__PHP_Incomplete_Class_Name" errors when using classes deserialized from the session. So you need to make sure the spl_autoload_register is done BEFORE session_start() is called. CORRECT:
When using spl_autoload_register() with class methods, it might seem that it can use only public methods, though it can use private/protected methods as well, if registered from inside the class: Output: -------- Trying to load Class1 via ClassAutoloader::loader() Class1::__construct() Trying to load Class2 via ClassAutoloader::loader() Class2::__construct()
If your autoload function is a class method, you can call spl_autoload_register with an array specifying the class and the method to run. * You can use a static method : * Or you can use an instance :
Think twice about throwing an exception from a registered autoloader. If you have multiple autoloaders registered, and one (or more) throws an exception before a later autoloader loads the class, stacked exceptions are thrown (and must be caught) even though the class was loaded successfully.
What I said here previously is only true on Windows. The built-in default autoloader that is registered when you call spl_autoload_register() without any arguments simply adds the qualified class name plus the registered file extension (.php) to each of the include paths and tries to include that file. Example (on Windows): include paths: - "." - "d:/projects/phplib" qualified class name to load: network\http\rest\Resource Here's what happens: PHP tries to load '.\\network\\http\\rest\\Resource.php' -> file not found PHP tries to load 'd:/projects/phplib\\network\\http\\rest\\Resource.php' -> file found and included Note the slashes and backslashes in the file path. On Windows this works perfectly, but on a Linux machine, the backslashes won't work and additionally the file names are case-sensitive. That's why on Linux the quick-and-easy way would be to convert these qualified class names to slashes and to lowercase and pass them to the built-in autoloader like so:
鹏仔微信 15129739599 鹏仔QQ344225443 鹏仔前端 pjxi.com 共享博客 sharedbk.com
图片声明:本站部分配图来自网络。本站只作为美观性配图使用,无任何非法侵犯第三方意图,一切解释权归图片著作权方,本站不承担任何责任。如有恶意碰瓷者,必当奉陪到底严惩不贷!