In the PHP development process, if you want to introduce a class from the outside, you will typically use the include and require methods to include the file that defines this class, but this may make the new script that references the file There is a large number of include or require method calls, if the inadvertent omission will produce errors, making the code difficult to maintain.
Since PHP5, the __autoload interceptor method has been introduced to automatically include references to class files, which we usually write:
Copy Code code as follows:
function __autoload ($className) {
Include_once $className. '. class.php ';
}
$user = new User ();
When the PHP engine attempts to instantiate an operation of an unknown class, it invokes the __autoload () method, which has the last chance to load the class required before the PHP error fails. So, when the above code executes, the PHP engine actually automatically executes a __autoload method for us, including the User.class.php file.
Exceptions thrown in the __autoload function cannot be caught by a catch statement block and cause a fatal error.
If you use the CLI interactive mode of PHP, the automatic loading mechanism will not execute.
When you want to use pear-style naming conventions, such as the need to introduce user/register.php files, you can do so:
Copy Code code as follows:
Load Me
function __autoload ($className) {
$file = Str_replace (' _ ', Directory_separator, $className);
Include_once $file. ' PHP ';
}
$userRegister = new User_register ();
Although this method is convenient, but in a large application if the introduction of a number of class libraries, may be due to the different types of library autoload mechanism to produce some inexplicable problems. After PHP5 introduced the SPL standard library, we have a new solution, the Spl_autoload_register () function.
The function is to register the function into the SPL __autoload function stack and remove the system default __autoload () function. Once the Spl_autoload_register () function is invoked, all functions registered to the Spl_autoload_register () function are called sequentially instead of the __autoload () function automatically called when the class is called without a definition. The following example calls user/register.php instead of User_Register.class.php:
Copy Code code as follows:
Don't load Me
function __autoload ($className) {
Include_once $className. '. class.php ';
}
Load Me
function AutoLoad ($className) {
$file = Str_replace ('/', Directory_separator, $className);
Include_once $file. '. php ';
}
Start loading
Spl_autoload_register (' AutoLoad ');
$userRegister = new User_register ();
When using Spl_autoload_register (), we can also consider a more secure initialization invocation method, as follows:
Copy Code code as follows:
System default __autoload function
function __autoload ($className) {
Include_once $className. '. class.php ';
}
__autoload functions that can be loaded by SPL
function AutoLoad ($className) {
$file = Str_replace (' _ ', Directory_separator, $className);
Include_once $file. '. php ';
}
Accidentally loaded the wrong function name, but also the default __autoload mechanism to cancel ... 囧
Spl_autoload_register (' _autoload ', false);
Fault-tolerant mechanism
if (false = = Spl_autoload_functions ()) {
if (function_exists (' __autoload ')) {
Spl_autoload_register (' __autoload ', false);
}
}
Chine: In the Unix/linux environment, if you have a number of smaller classes, in order to manage the convenience, are written in a PHP file, you can through the ln-s command to do a soft link to quickly distribute a number of different class name copies, and then through the autoload mechanism to load.