Summary of autoload usage in PHP _php tips

Source: Internet
Author: User
Tags autoload zend

PHP provides autoload to help us facilitate the inclusion of files, but AutoLoad is not as capable of handling all the situations as it is, and today it's time to take a look at some of the autoload problems that have been encountered in the past few days.

Why to use AutoLoad

When using classes in PHP, we have to load them before use, whether through require or include, but there are two issues that affect our decision to load.

First of all, do not know where this type of file is stored, the other is that you do not know when to use this file. In particular, the project file is particularly long, it is not possible to each file in the beginning of the section to write a series of require ....

After PHP5, we can solve this problem by __autoload. And after PHP5.1, Spl_autoload_register () is also 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 via new, PHP loads the corresponding file with the defined __autoload function if the class file uses extends or imp Lements need to use other class files, PHP will rerun autoload to do the search and loading of class files, if two of the same type of file request, it will be an error. The original author provides three interesting examples to illustrate this problem, you can download the source view here.

In general, there are a number of ways to resolve the method of locating files at the appropriate location at load time. The most common use is to specify a specific naming standard.

The method 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 will be replaced with the delimiters in the path, and the main/super/class.php file will be removed from the example above

The disadvantage of this approach is that in the coding process, we have to know exactly where the code file should be, and because

The file path is hard-coded into 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

This is handy if you're in a development environment and you don't care about speed. By placing all class files in one or several specific folders, and then looking up the load by traversing them.

For example:

Copy Code code as follows:

<?php
$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 establish an associated configuration file between the class file and his location, for example:

Copy Code code as follows:

configuration.php
Array_of_associations = Array (
' Mainsuperclass ' = ' c:/main/super/class.php ',
' Mainpoorclass ' = ' c:/blablabla/gy.php '
);

The file called
Copy Code code as follows:

<?php
Require ' autoload_generated.php ';

function __autoload ($className) {
Global $autoload _list;
Require_once $autoload _list[$className];
}

$x = new A ();
?>


Of course, if the file is a lot of time, maintenance will be a hassle, but with the class name in the hard-coded position, which is better?

We certainly do not want to manually maintain this list, then we 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 implements such a tool, careful consideration, this is not difficult to achieve, the original author even developed a small autoload framework, worth learning.

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.