During PHP development, if you want to introduce a class from the outside, the include and require methods are usually used to include the file defining this class, however, this may cause a large number of include or require method calls in the new script of the referenced file. If you miss it for a moment, an error will occur, making the code difficult to maintain.
Since PHP5, the _ autoload interceptor method is introduced to automatically include and reference class files. We usually write this as follows:
Copy codeThe Code is as follows:
Function _ autoload ($ className ){
Include_once $ className. '. class. php ';
}
$ User = new User ();
When the PHP engine tries to instantiate an operation of an unknown class, it will call the _ autoload () method, and there is a last chance to load the required class before the PHP error fails. Therefore, when the above code is executed, the PHP engine automatically executes the _ autoload Method for us to include the file User. class. php.
The exception thrown in the _ autoload function cannot be caught by the catch statement block and cause a fatal error.
If you use the CLI interaction mode of PHP, the automatic loading mechanism will not be executed.
If you want to use PEAR-style naming rules, such as introducing the User/Register. php file, you can do the following:
Copy codeThe Code is as follows:
// Load me
Function _ autoload ($ className ){
$ File = str_replace ('_', DIRECTORY_SEPARATOR, $ className );
Include_once $ file. 'php ';
}
$ UserRegister = new User_Register ();
This method is convenient, but when multiple class libraries are introduced in a large application, some inexplicable problems may occur due to the autoload mechanism of different class libraries. After PHP5 introduced the SPL standard library, we added a new solution, the spl_autoload_register () function.
This function registers the function to the _ autoload function stack of SPL and removes the default _ autoload () function. Once the spl_autoload_register () function is called, The system calls all functions registered to the spl_autoload_register () function in sequence, instead of automatically calling the _ autoload () function, the following example calls User/Register. php instead of User_Register.class.php:
Copy codeThe Code is as follows:
// Do not 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 using a more secure initialization call method. refer to the following:
Copy codeThe Code is as follows:
// System default _ autoload Function
Function _ autoload ($ className ){
Include_once $ className. '. class. php ';
}
// The _ autoload function 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, and canceled the default _ autoload mechanism ...... Bytes
Spl_autoload_register ('_ autoload', false );
// Fault Tolerance Mechanism
If (false === spl_autoload_functions ()){
If (function_exists ('_ autoload ')){
Spl_autoload_register ('_ autoload', false );
}
}
Amazing: in Unix/Linux environments, if you have multiple small-sized classes, you can write them in a PHP file for ease of management, you can use the ln-s command as a soft link to quickly distribute copies of multiple different class names, and then load them using the autoload mechanism.