Several ways of PHP's autoload automatic loading mechanism

Source: Internet
Author: User
Tags autoload spl

Talk about PHP's autoload automatic loading mechanism

PHP's autoload can be used in roughly two ways: __autoload and SPL methods. There are different ways of using these two methods

How to use __autoload 1:
The most common use of this method, according to the class name, find the class file, and then Require_one

Copy the Code code as follows:


function __autoload ($class _name) {
$path = Str_replace (' _ ', '/', $class _name);
Require_once $path. '. php ';
}
This will automatically load the http/file/interface.php file
$a = new Http_file_interface ();


The benefits of this approach are simple and easy to use. Of course there are shortcomings, the disadvantage is that the class name and file path is forced to make a convention, when the file structure is modified, it is bound to modify the class name.

How to use __autoload 2 (direct mapping method)

Copy the Code code 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];
}
}
This will automatically load the c:/php/http/file/interface.php file
$a = new Http_file_interface ();



The advantage of this approach is that the class name and file path are only maintained with a single map, so when the file structure changes, you do not need to modify the class name, just modify the corresponding item in the map.

The disadvantage of this approach compared to the previous method is that when the file is much more difficult to maintain, perhaps you will consider using JSON or a single file for maintenance. Perhaps you would have thought of using a framework to maintain or build such a mapping.

Spl_autoload

The biggest flaw in __autoload is the inability to have multiple autoload methods

Well, think of the following scenario, your project refers to someone else's project, your project has a __autoload, other people's project also has a __autoload, so that two __autoload conflict. The solution is to modify the __autoload to become one, which is undoubtedly very cumbersome.

So we urgently need to use a autoload call stack so that SPL's AutoLoad series functions appear. You can use Spl_autoload_register to register multiple custom autoload functions

If your PHP version is greater than 5.1, you can use the Spl_autoload

Learn some of the functions of SPL first:


Spl_autoload is the default implementation of _autoload (), which will go to include_path for $class_name (. php/.inc)
Spl_autoload for automatic loading:

Copy the Code code as follows:


/*http.php*/
<?php
Class HTTP
{
Public Function Callname () {
echo "This is HTTP";
}
}
/*test.php*/
<?php
Set_include_path ("/home/yejianfeng/handcode/"); Here you need to put the path into the include
Spl_autoload ("http"); Find/home/yejianfeng/handcode/http.php
$a = new http ();
$a->callname ();



Spl_autoload_register

Register the function in the SPL __autoload function stack and look directly at an example:

Copy the Code code as follows:


/*http.php*/
<?php
Class HTTP
{
Public Function Callname () {
echo "This is HTTP";
}
}

/*test.php*/
<?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 to see the following example

Copy the Code code as follows:


/*http.php*/
<?php
Class HTTP
{
Public Function Callname () {
echo "This is HTTP";
}
}
/*http2.php*/
<?php
Class HTTP
{
Public Function Callname () {
echo "This is HTTP2";
}
}

/*test.php*/
<?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 time it will output "This is HTTP2"


Spl_auto_register This function makes it possible to use a custom function for automatic loading without using __autoload. This method is now used frequently.
This method is used by the Zend Autoloader module. Extract the corresponding code

Copy the Code code as follows:


Spl_autoload_register (Array (__class__, ' autoload '));

public static function AutoLoad ($class)
{
.....

}


Several ways of PHP's autoload automatic loading mechanism

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.