Home >Backend Development >PHP Tutorial >PHP function spl_autoload_register() usage and __autoload() introduction_PHP tutorial
I won’t talk about the usage of __autoload() anymore, I have mentioned it before in my WEB development notes. How to use PHP __autoload function (automatically load class files), original address: http://www.jb51.net/article/29625.htm.
Let’s talk about the usage of spl_autoload_register(). It’s very simple. It can be understood like this, which is to declare a custom __autoload(). It can be function A or function B. You can do whatever you want. Of course, the function body must be written in the same way as __autoload().
This method will be called when PHP cannot find the class file. When registering its own function or method, PHP will not call the __autoload() function, but will call the custom function
spl_autoload_register('func_name' ; () Function
Description
bool spl_autoload_register ([ callback $autoload_function ] )
Register the function into the SPL __autoload function stack. Activate functions in this stack if they are not already active.
If the __autoload function has been implemented in your program, it must be explicitly registered in the __autoload stack. Because the
spl_autoload_register() function will replace the __autoload function in Zend Engine with spl_autoload() or
spl_autoload_call().
Parameters
autoload_function
The autoload function to be registered. If no parameters are provided, the default implementation function
spl_autoload() of autoload is automatically registered.
Return value
Returns TRUE if successful, FALSE if failed.
Note: SPL is the abbreviation of Standard PHP Library. It is an extension library introduced in PHP5. Its main functions include the implementation of the autoload mechanism and various Iterator interfaces or classes. The SPL autoload mechanism is implemented by pointing the function pointer autoload_func to a self-implemented function with autoloading function. SPL has two different functions spl_autoload and spl_autoload_call. Different automatic loading mechanisms are implemented by pointing autoload_func to these two different function addresses.
The code is as follows:
test.class.php
Copy code
The code is as follows: