PHP provides autoload to help us to facilitate the inclusion of the file, but AutoLoad is not imagined to be able to handle all situations, today to record a few days ago encountered the AutoLoad some problems.
Why to use Autoload
When we use classes in PHP, we have to load them before we use them, either through require or include, but there are two problems that affect the decision we make to load.
The first thing is that you don't know where the file is stored, and the other is not sure when you need to use it. Especially for project files, it is not possible to write a long string of require in the beginning of each file ....
After PHP5, we can solve this problem by __autoload. And after the PHP5.1, Spl_autoload_register () is provided to provide a more complete loading mechanism.
By reading the autoloading in PHP article, I understand the Autoload loading mechanism, when instantiating a class through new, PHP loads the corresponding file by defining the __autoload function, if the class file uses the extends or imp Lements need to use other class files, PHP will rerun autoload to find and load the class file, if two times the same type of file request, will be an error. The original author provides three interesting examples to illustrate this problem, which can be downloaded from here to see the source.
In general, there are a number of ways to troubleshoot how to find files at the appropriate location when loading. The most common use is to specify a specific naming standard.
Methods of Zend
Zend recommends one of the most popular ways to include a path in the file name. For example, the following example:
Main.class
function __autoload ($class _name) {
$path = Str_replace (' _ ', Directory_separator, $class _name);
Require_once $path. " PHP ';
}
$temp = new Main_super_class (); All underscores are replaced with delimiters in the path, and the previous example goes to the main/super/class.php file
The disadvantage of this approach is that in the coding process, we have to know exactly where the code file should be located, and because
The file path is hard-coded in the class name, and if you need to modify the structure of the folder, we must manually modify all the class names.
' Include All ' method
If it is in a development environment, and is not very concerned about speed, it is very convenient to use this method. By placing all the class files in one or several specific folders, and then looking through the load.
For example:
Copy the Code code as follows:
$arr = Array (
' Project/classes ',
' 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;
}
}
?>
Associating files and Locations
Another method is to create an associated configuration file between the class file and his location, for example:
Copy the Code code as follows:
configuration.php
Array_of_associations = Array (
' Mainsuperclass ' = ' c:/main/super/class.php ',
' Mainpoorclass ' = ' c:/blablabla/gy.php '
);
The called file
Copy the Code code as follows:
Require ' autoload_generated.php ';
function __autoload ($className) {
Global $autoload _list;
Require_once $autoload _list[$className];
}
$x = new A ();
?>
Of course, it can be a hassle to maintain when there are a lot of files, but which is better than hard-coded locations in class names?
We certainly do not want to manually maintain this list, then you can use the automatic generation of this file to implement, the corresponding relationship of the file can be Php\xml\json and so on. The author of the original text realized a tool like this, it is not difficult to think about it, the original author even developed a small Autoload framework, worth learning.
http://www.bkjia.com/PHPjc/825066.html www.bkjia.com true http://www.bkjia.com/PHPjc/825066.html techarticle PHP provides autoload to help us to facilitate the inclusion of the file, but AutoLoad is not imagined to be able to handle all situations, today to record the first few days encountered in the AU ...