PHP autoload can use roughly two methods: __autoload and SPL methods. There are several different ways to use these two methods.
Method 1 for __autoload: The most commonly used method is to find the class file based on the class name and require_one
1
2
3
4
5
6
7
8
function __autoload($class_name) {
$path = str_replace('_', '/', $class_name);
require $path . '.php';Copy the code
}
// The Http/File/ interface-.php File is automatically loaded
$a = new Http_File_Interface(); The advantage of this method is that it is simple and easy to use. Of course, there are disadvantages, the disadvantage is the class name and file path forced to do the agreement, when modifying the file structure, it is necessary to modify the class name.
$map = array($map = array($map = array($map = array($map = array))
'Http_File_Interface' => 'C:/PHP/HTTP/FILE/Interface.php'Copy the code
);
function __autoload($class_name) {
if (isset($map[$class_name])) {
require $map[$class_name];
}Copy the code
}
/ / here will be automatically loaded C: / PHP/HTTP/FILE/Interface. The PHP FILE
$a = new Http_File_Interface();
1
1
1 The advantage of this approach is that the class name and file path are only maintained by a map, so when the file structure changes, there is no need to change the class name, just need to change the corresponding items in the map. The disadvantage of this approach is that it can be very difficult to maintain when there are too many files, so you might want to use JSON or a separate file for maintenance. You might think of using a framework to maintain or create such a mapping.
Spl_autoload __autoload’s biggest drawback is the inability to have more than one Autoload method
Well, consider the following scenario, where your project refers to someone else’s project, and your project has an __autoload, and someone else’s project has an __autoload, so the two __autoloads conflict. The solution is to modify __autoload to become one, which is undoubtedly very tedious.
Therefore, we urgently need to use an Autoload call stack, so that SPL’s Autoload family of functions appears. You can use spl_autoload_register to register multiple custom Autoload functions
If your PHP version is larger than 5.1, you can use spl_autoload
Let’s start with a few functions of SPL:
clip_image001
Spl_autoload is the default implementation of _autoload(), which looks for $class_name(.php/.inc) in the include_path.
Spl_autoload: 12 3 4 5 6 7 8 9 10 11 12 13 14 15 16 /http.php/
public function callname(){
echo "this is http ";
} Copy the code
}
/test.php/
$a->callname();
Spl_autoload_register registers functions to the SPL __autoload function stack.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 /http.php/
public function callname(){
echo "this is http ";
} Copy the code
}
/test.php/
if($class == 'http'){
require_once("/home/yejianfeng/handcode/http.php ");
} Copy the code
});
a=newhttp(); a->callname();
Spl_autoload_call calls the calling function registered in spl_autoload_register, as shown in the following example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 /http.php/
public function callname(){
echo "this is http ";
} Copy the code
}
/http2.php/
public function callname(){
echo "this is http2 ";
} Copy the code
}
/test.php/
if($class == 'http'){
require_once("/home/yejianfeng/handcode/http.php ");
}
if($class == 'http2'){
require_once("/home/yejianfeng/handcode/http2.php ");
}Copy the code
});
spl_auto_call(‘http2’);
$a = new http();
$a->callname(); // This is http2
The spl_auto_register function makes it possible to use custom functions for auto-loading instead of __autoload. This method is now commonly used.
Zend’s AutoLoader module uses this approach. Extract the corresponding code
1
2
3
4
5
6
7
spl_autoload_register(array(__CLASS__, ‘autoload’));
public static function autoload($class)
{
... .Copy the code
}
This article from porch vein blade blog garden, the original link: www.cnblogs.com/yjf512/arch… , please contact the original author if necessary