Example Explanation of Automatic Loading of Classes in php Project
- 2021-12-21 04:15:58
- OfStack
Main function: spl_autoload_register ()-Registers the given function as an implementation of __autoload ()
Register the function in the SPL __autoload function queue. If the functions in the queue have not been activated, they are activated.
If the __autoload () function has been implemented in your program, it must be explicitly registered in the __autoload () queue. Because the spl_autoload_register () function replaces the __autoload () function in Zend Engine with spl_autoload () or spl_autoload_call ().
If multiple autoload functions are required, spl_autoload_register () satisfies such requirements. It actually creates a queue of autoload functions, which are executed one by one in the order in which they were defined. In contrast, __autoload () can only be defined once.
<?php
// $class Class name
function autoloader_1($class) {
include 'classes/' . $class . '.class.php';
}
function autoloader_2($class) {
include 'classes/' . $class . '.class.php';
}
// Can be used multiple times, but __autoload() Function can only use the 1 Times.
spl_autoload_register('autoloader_1');
spl_autoload_register('autoloader_2');
// Or, since PHP 5.3.0 Can be used from now on 1 Anonymous function
spl_autoload_register(function ($class) {
include 'classes/' . $class . '.class.php';
});
The above is all the relevant knowledge points, thank you for your study and support for this site.