PHP provides Autoload to help us easily include files, but autoload does not handle all situations as imagined, today, we will record some problems with autoload that we encountered a few days ago.
Why use Autoload?
When using a class in PHP, We must load the class before use, whether in the require or include mode, but there are two problems that affect our decision on loading.
First, I don't know where the class file is stored, and the other is I don't know when to use it. In particular, when there are too many project files, it is impossible for each file to write a long string of require ....
After PHP5, we can solve this problem through _ autoload. In addition, after PHP5.1, spl_autoload_register () is also provided to provide a better loading mechanism.
After reading the Autoloading in PHP article, I understand the Autoload loading mechanism. When a class is instantiated through new, PHP will load the corresponding file through the defined _ autoload function, if this class file uses extends or implements and requires other class files, php will re-run autoload to search and load class files, if two requests are made to the same class of files, an error is returned. The original author provides three interesting examples to illustrate this problem. You can download the source code here.
In general, there are many ways to find files at the corresponding location during loading. The most commonly used is to specify a specific naming standard.
Zend Method
Zend recommends the most popular method to include a path in a file name. For example:
// Main. class
Function _ autoload ($ class_name ){
$ Path = str_replace ('_', DIRECTORY_SEPARATOR, $ class_name );
Require_once $ path. '. php ';
}
$ Temp = new Main_Super_Class (); All underlines are replaced with separators in the path. In the preceding example, the Main/Super/Class. php file is used.
The disadvantage of this method is that in the coding process, we must clearly know the location of the code file.
The file path is hardcoded in the class name. If you need to modify the structure of the folder, you must manually modify all the class names.
'Include all' Method
If you are in a development environment and are not very concerned about the speed, it is very convenient to use this method. By placing all class files in one or more specific folders, and then searching and loading through traversal.
For example:
Copy codeThe Code is as follows:
<? Php
$ Arr = array (
'Project/class ',
'Project/Classes/Children ',
'Project/interfaces'
);
Foreach ($ arr as $ dir ){
$ Dir_list = opendir ($ dir );
While ($ file = readdir ($ dir_list )){
$ Path = $ dir. DIRECTORY_SEPARATOR. $ file;
If (in_array ($ file, array ('.', '..') | is_dir ($ path ))
Continue;
If (strpos ($ file, ". class. php "))
Require_once $ path;
}
}
?>
Associated files and locations
Another method is to establish an associated configuration file between the class file and its location, for example:
Copy codeThe Code is as follows:
// Configuration. php
Array_of_associations = array (
'Mainsuperclass' = 'C:/Main/Super/Class. php ',
'Mainpoorclass' = 'C:/blablabla/gy. php'
);
Called File
Copy codeThe Code is as follows:
<? Php
Require 'autoload _ generated. php ';
Function _ autoload ($ className ){
Global $ autoload_list;
Require_once $ autoload_list [$ className];
}
$ X = new ();
?>
Of course, if there are a lot of files, maintenance will be a headache, but which is better than hard encoding in the class name?
Of course, we do not want to manually maintain this list, so we can use automatic generation of this file. The file corresponding to this link can be php \ xml \ json and so on. The author of the original article has implemented such a tool. If you think about it carefully, it is not very difficult to implement it. The author of the original article even developed a small Autoload framework, which is worth learning.