Differences between spl_autoload_register and autoload
Source: Internet
Author: User
This article provides a detailed analysis of the differences between spl_autoload_register and autoload. For more information, see Spl_autoload_register (PHP 5> = 5.1.2)
Spl_autoload_register-register _ autoload () function
Description Bool spl_autoload_register ([callback $ autoload_function])
Register the function to the SPL _ autoload function stack. If the functions in the stack are not activated, activate them.
If the _ autoload function has been implemented in your program, it must be explicitly registered to the _ autoload stack. Because
The spl_autoload_register () function replaces the _ autoload function in Zend Engine with spl_autoload () or
Spl_autoload_call ().
Parameters Autoload_function
The automatically loaded function to be registered. If no parameter is provided, the default function of autoload is automatically registered.
Spl_autoload ().
Return value If the call succeeds, TRUE is returned. if the call fails, FALSE is returned.
Note: SPL is the abbreviation of Standard PHP Library (Standard PHP Library. It is an extension library introduced by PHP5. its main functions include the implementation of the autoload mechanism and various Iterator interfaces or classes. The implementation of the SPL autoload mechanism is achieved by pointing the function pointer autoload_func to the self-implemented function with the automatic loading 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 function addresses.
Example We have A class file named A. php, which defines A class named:
The code is as follows:
Class
{
Public function _ construct ()
{
Echo 'Got it .';
}
}
Then we have an index. php that needs to use this Class A. The General syntax is
The code is as follows:
Require ('A. php ');
$ A = new ();
However, there is a problem, for example, our index. php needs to include not only Class A, but many classes. in this way, many lines of require statements must be written, which sometimes makes people feel uncomfortable.
However, in versions later than php5, we no longer need to do this. In php5, the autoload function is automatically called when you try to use a class that has not yet been defined. Therefore, you can compile the _ autoload function to allow php to automatically load classes, instead of writing a long list of contained files.
For example, in the above example, index. php can be written as follows: The code is as follows:
Function _ autoload ($ class)
{
$ File = $ class. '. php ';
If (is_file ($ file )){
Require_once ($ file );
}
}
$ A = new ();
Of course, the above is just the simplest example, __autoload just goes to include_path to find the class file and load it. we can define the _ autoload loading class rules as needed.
In addition, if we do not want to call _ autoload when loading automatically, but call our own function (or class method), we can Use spl_autoload_register to register our own autoload function. Its function prototype is as follows: Bool spl_autoload_register ([callback $ autoload_function])
We will continue to rewrite the example above: The code is as follows:
Function loader ($ class)
{
$ File = $ class. '. php ';
If (is_file ($ file )){
Require_once ($ file );
}
}
Spl_autoload_register ('loader ');
$ A = new ();
This way, php can run normally. at this time, when looking for classes, php does not call _ autoload, but calls our own function loader. In the same way, the following statements can also be used:
The code is as follows:
Class Loader
{
Public static function loadClass ($ class)
{
$ File = $ class. '. php ';
If (is_file ($ file )){
Require_once ($ file );
}
}
}
Spl_autoload_register (array ('loader ', 'loadclass '));
$ A = new ();
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.
A Free Trial That Lets You Build Big!
Start building with 50+ products and up to 12 months usage for Elastic Compute Service