Home >Backend Development >PHP Tutorial >Simple usage tutorial of autoLoad automatic loading mechanism
php's autoload can generally use two methods: autoload and spl methods. These two methods have several different usage methods.
Usage method of autoload 1:
The most commonly used method is to find the class based on the class name. file, and then require_one
The code is as follows:
function autoload($class_name) { $path = str_replace('_', '/', $class_name); require_once $path . '.php'; } // 这里会自动加载Http/File/Interface.php 文件 $a = new Http_File_Interface();
The advantage of this method is that it is simple and 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 the file structure is modified, the class name must be modified.
Autoload usage method 2 (direct mapping method)
The 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]; } } // 这里会自动加载C:/PHP/HTTP/FILE/Interface.php 文件 $a = new Http_File_Interface();
The advantage of this method is Class names and file paths are only maintained using a mapping, so when the file structure changes, there is no need to modify the class name, only the corresponding items in the mapping need to be modified.
The disadvantage of this method compared to the previous method is that it is very troublesome to maintain this mapping when there are too many files. Maybe at this time you will consider using json or a separate file for maintenance. Maybe you would think of using a framework to maintain or create such a mapping.
spl_autoload
The biggest flaw of autoload is that it cannot have multiple autoload methods
Okay, think about the following scenario, your project references If you have copied someone else's project, your project has an autoload, and someone else's project also has an autoload, so the two autoloads will conflict. The solution is to modify autoload to become one, which is undoubtedly very cumbersome.
Therefore we urgently need to use an autoload call stack, so that the autoload series functions of spl 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 spl_autoload
First understand several functions of spl:
spl_autoload is the default implementation of _autoload(), it will look for $class_name(.php/.inc) in include_path
Spl_autoload implements automatic loading:
The code is as follows:
/*http.php*/ <?php class http { public function callname(){ echo "this is http"; } } /*test.php*/ <?php set_include_path("/home/yejianfeng/handcode/"); //这里需要将路径放入include spl_autoload("http"); //寻找/home/yejianfeng/handcode/http.php $a = new http(); $a->callname();
Spl_autoload_register
Register the function into the SPL autoload function stack, look at an example directly:
The code is 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 calling function registered in spl_autoload_register, see the example below
The code is 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 is http2"
spl_auto_registerThis function makes We do not use autoload, it is possible to use custom functions for automatic loading. This method is now commonly used.
Zend’s AutoLoader module uses this method. Excerpt the corresponding code
The code is as follows:
spl_autoload_register(array(CLASS, 'autoload')); public static function autoload($class) { ….. }
The above is the detailed content of Simple usage tutorial of autoLoad automatic loading mechanism. For more information, please follow other related articles on the PHP Chinese website!