About the PHP autoLoad automatic loading mechanism

Source: Internet
Author: User
Tags autoload spl
Php autoload can be used in two ways: __autoload and spl. The two methods have different usage methods. _ Autoload usage Method 1:
This method is most often used. find the class file based on the class name, and then require_one
Copy codeThe code is as follows:
Function _ autoload ($ class_name ){
$ Path = str_replace ('_', '/', $ class_name );
Require_once $ path. '. php ';
}
// The Http/File/Interface. php File is automatically loaded here.
$ A = new Http_File_Interface ();

The advantage of this method is that it is easy to use. Of course, there are also disadvantages. The disadvantage is that the class name and file path are forced to be agreed. when modifying the file structure, it is necessary to modify the class name.

_ Autoload method 2 (direct ing)
Copy codeThe code is as follows:
$ Map = array (
'Http _ File_Interface '=> 'C:/PHP/Http/FILE/Interface. php'
);
Function _ autoload ($ class_name ){
If (isset ($ map [$ class_name]) {
Require_once $ map [$ class_name];
}
}
// The C:/PHP/HTTP/FILE/Interface. php FILE is automatically loaded here.
$ A = new Http_File_Interface ();


The advantage of this method is that the class name and file path are only maintained by a ing. Therefore, when the file structure changes, you do not need to modify the class name, you only need to modify the corresponding items in the ING.

The disadvantage of this method is that it is very troublesome to maintain the ing when there are too many files. you may consider using json or a single file for maintenance. You may want to use a framework to maintain or establish such a ING.

Spl_autoload

_ The biggest defect of autoload is that there cannot be multiple autoload methods

Now, let's look at the following scenario. your project references another project. your project has a _ autoload, and other projects have a _ autoload, in this way, the two _ autoloads conflict. The solution is to change _ autoload into one, which is undoubtedly very tedious.

Therefore, we urgently need to use an autoload call stack, so that the spl autoload series functions will appear. You can use spl_autoload_register to register multiple custom autoload functions.

If your PHP version is later than 5.1, you can use spl_autoload

First, let's look at several functions of spl:


Spl_autoload is the default implementation of _ autoload (). it searches for $ class_name (. php/. inc) in include_path)
Spl_autoload for automatic loading:
Copy codeThe code is as follows:
/* Http. php */
Class http
{
Public function callname (){
Echo "this is http ";
}
}
/* Test. php */
Set_include_path ("/home/yejianfeng/handcode/"); // put the path to include
Spl_autoload ("http"); // search for/home/yejianfeng/handcode/http. php
$ A = new http ();
$ A-> callname ();


Spl_autoload_register

Register the function to the SPL _ autoload function stack. let's look at an example:
Copy codeThe code is as follows:
/* Http. php */
Class http
{
Public function callname (){
Echo "this is http ";
}
}

/* Test. php */
Spl_autoload_register (function ($ class ){
If ($ class = 'http '){
Require_once ("/home/yejianfeng/handcode/http. php ");
}
});

$ A = new http ();
$ A-> callname ();


Spl_autoload_call

Call the call function registered in spl_autoload_register. See the following example.
Copy codeThe code is as follows:
/* Http. php */
Class http
{
Public function callname (){
Echo "this is http ";
}
}
/* Http2.php */
Class http
{
Public function callname (){
Echo "this is http2 ";
}
}

/* Test. php */
Spl_autoload_register (function ($ class ){
If ($ class = 'http '){
Require_once ("/home/yejianfeng/handcode/http. php ");
}
If ($ class = 'http2 '){
Require_once ("/home/yejianfeng/handcode/http2.php ");
}
});
Spl_auto_call ('http2 ');
$ A = new http ();
$ A-> callname (); // "this is http2" will be output at this time"

The spl_auto_register function makes it possible to use a custom function for automatic loading instead of _ autoload. This method is frequently used now.
This method is used by the Zend AutoLoader module. Extract the corresponding code
Copy codeThe code is as follows:
Spl_autoload_register (array (_ CLASS __, 'autoload '));

Public static function autoload ($ class)
{
.....

}


Reference:
For the autoload mechanism of zend, the http://www.bitsCN.com/article/31399.htm of the previous article has a detailed analysis.
For more information about the automatic loading mechanism of autoload, see http://www.bitscn.com/article/31279.htm.

Contact Us

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

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.