PHP provides Autoload to help us easily include files, but Autoload is not as good as you think it can handle all the cases, today to record a few days ago encountered Autoload there are some problems.

Why use Autoload.

When using a class in PHP, we must load it before using it, either through require or include, but there are two issues that affect our decision to load it.

One is not knowing where the class file is stored, and another is not knowing when you need it. Especially if the project files are very long, it is not possible to write a long require at the beginning of every file… .

After PHP5, we can solve this problem with __autoload. And since PHP5.1, spl_Autoload_register () has been provided to provide a more complete loading mechanism.

When instantiating a class using new, PHP loads the file using its __autoload function. If the class file uses extends or implements and requires another class file, PHP reruns Autoload to find and load the class file. If two requests for the same class file occur, an error is reported. The author provides three interesting examples to illustrate this problem, which can be downloaded here.

In general, there are several ways to solve the problem of finding files at the appropriate location at load time. Most commonly used is to specify specific naming standards.

Zend method.

One of the most popular approaches recommended by Zend is to include the path in the 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(); 
Copy the code

All underscores are replaced with delimiters in the path, which in our example goes to the Main/Super/ class.php file

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

The file path is hardcoded into the class name, so if we need to change the structure of the folder, we have to manually change all the class names.

‘Include All’ method

This is handy if you are in a development environment where speed is not a big concern. Find the load by placing all the class files in one or more specific folders and walking through them.

Such as:Copy the code

  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 create a configuration file associated with the class file and its location, for example:

// configuration.php

array_of_associations = array(

‘MainSuperClass’ = ‘C:/Main/Super/Class.php’,

‘MainPoorClass’ = ‘C:/blablabla/gy.php’

);

Invoked file

  function __autoload($className) {

global $autoload_list;

require_once $autoload_list[$className];

}

$x = new A(); ? >

Of course, it can be a hassle to maintain if there are too many files, but is it better to hardcode locations in class names? 🙂

We certainly don’t want to maintain this list manually, so we can do this by automatically generating this file. The corresponding file can be PHP \ XML \json, etc. The author of the original has implemented such a tool, which is not too difficult to implement if you think about it. The author of the original has even developed a small Autoload framework that is worth studying.

References:

1, Autoloading in PHP

3, Autoload problem with static variables 4, Class autoloades earlier when using APC

5. PHP AutoLoad Best Practices

Technorati tags: PHP,AUTOLOAD,ZEND,Name Standard