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 there are two of requests for the same type of file, it will error
Automatic loading principle
When developing a system using the OO pattern in PHP, it is often customary to store implementations of each class in a separate file, which makes it easy to reuse classes and facilitates future maintenance. This is also one of the basic ideas of OO design. Before PHP5, if you need to use a class, just use Include/require to include it in it. The following is a practical example:
* Person.class.php * *
<?php
Class Person {
var $name, $age;
function __construct ($name, $age)
{
$this->name = $name;
$this->age = $age;
}
}
?>
* no_autoload.php * *
<?php
Require_once ("Person.class.php");
$person = new Person ("Altair", 6);
Var_dump ($person);
?>
In this case, the no-autoload.php file needs to use the person class, which uses require_once to include it, and then it can instantiate an object directly using the person class.
But as the scale of the project expands, there are some hidden problems with this approach: if a PHP file needs to use many other classes, then a lot of require/include statements are required, which may cause omission or include unnecessary class files. If a large number of files require the use of other classes, it must be a nightmare to ensure that each file contains the correct class file.
PHP5 provides a solution to this problem, which is the automatic loading (autoload) mechanism of the class. The autoload mechanism can make it possible for a PHP program to automatically include class files when using classes, rather than including all of the class files in the first place, a mechanism known as lazy loading.
The following is an example of using the autoload mechanism to load the person class:
/* autoload.php */
<?php
function __autoload ($classname) {
require_once ($classname. "class.php");
}
$person = new Person ("Altair", 6);
var_dump ($person);
?>
&nb SP; Typically, when a class is used, the __autoload () function is automatically run if the class is found to be PHP5, in which we can load the classes that need to be used. In our simple example, we directly add the class name with the extension ". class.php" to the class file name and then load it with require_once. From this example, we can see that autoload at least three things, the first thing is to determine the class file name based on the class name, the second thing is to determine the disk path where the class file is located (in our example is the simplest case, the class and the PHP program files that call them in the same folder), The third thing is to load the class from the disk file into the system. The third step is the simplest, only need to use include/require can be. To achieve the first step, the second step of the function, must be in the development of the class name and disk file mapping method, only then we can according to the class name to find its corresponding disk files.
Therefore, when there are a large number of class files to include, we only have to determine the appropriate rules, and then in the __autoload () function, the class name and the actual disk file, you can achieve the effect of lazy loading. From here we can also see that the most important implementation of the __autoload () function is the implementation of the class name and the actual disk file mapping rules.
But now the problem is, if in a system implementation, if you need to use a lot of other class libraries, these class libraries may be written by different developers, whose class name and the actual disk file mapping rules are different. If you want to implement the automatic loading of the class library file, you must implement all the mapping rules in the __autoload () function so that the __autoload () function may be very complex or even impossible to implement. The end result may be that the __autoload () function is bloated, which can have a significant negative impact on future maintenance and system efficiency even when implemented. In this case, there is no more simple and clear solution? The answer is of course: no! Before we look at further solutions, let's look at how the autoload mechanism in PHP is implemented.
(2) The implementation of PHP's autoload mechanism
To implement automatic loading, we need to use a function to implement:
/*
Autoload_function
Automatic load function to register. If no arguments are supplied, the default implementation function Spl_autoload () for the autoload is automatically registered.
Throw
This parameter sets whether Spl_autoload_register () throws an exception when Autoload_function is unable to register successfully.
Prepend
If True,spl_autoload_register () adds a function to the top of the queue, not the tail of the queue.
*/
BOOL Spl_autoload_register ([Callable $autoload _function [, bool $throw = True [, bool $prepend = false]]]
The most common way is through the Spl_autoload_register function to register the automatic loading class Method!
Register through Spl_autoload_register
We can use the Spl_autoload_register function to register the class method, before the class call, we determine whether the class file path is a file, if it is introduced!
Here is a small example:
<?php
Class AutoLoad
{
Publicstatic Functionload ($className)
{
$name = str_replace (' \ \ ', '/', $className);
$fileName = sprintf ('%s.php ', $name);
if (Is_file ($fileName)) {
Require_once $fileName;
}
Var_dump ($fileName);
}
}
It's okay to write that.
Spl_autoload_register (Array ("AutoLoad", "load"));
Spl_autoload_register ("Autoload::load");
Implemented by __autoload function
We create a new autoload.php file, which can be implemented with the following code:
<?php
Function__autoload ($className) {
$name = str_replace (' \ \ ', '/', $className);
$fileName = sprintf ('%s.php ', $name);
if (Is_file ($fileName)) {
Require_once ($fileName);
}
}
Summary
Automatic loading can make our operation easier, often need to automatically load some default configuration, so mastering the implementation of automatic loading is very important!