Description of autoload automatic loading mechanism in PHP

  • 2020-03-31 21:23:24
  • OfStack

In PHP development process, if you want to introduce a class from outside, often use the include and the require method, to define the class files included, but this may make in the new script reference file, the presence of large amounts of the include or require method calls, if the oversight missing may be an error, makes the code difficult to maintain.

After PHP5, the interceptor method of s/s has been introduced, which can automatically include a reference to the class file.
 
function __autoload($className) { 
include_once $className . '.class.php'; 
} 

$user = new User(); 

When the PHP engine tries to instantiate the operation of an unknown class, it calls the method of s/s (), giving it one last chance toload the required class before the PHP error fails. So, while the above code is executing, the PHP engine is actually doing one successive successive autoload method for us, including the user.class.php file.

The exception thrown in the successive autoload function cannot be caught by the catch block and cause a fatal error.

If you use PHP's CLI interaction mode, the auto-loading mechanism will not execute.

When you want to use the pearl-style naming rules, such as the User/ register.php file, you can also do this:
 
//Load I
function __autoload($className) { 
$file = str_replace('_', DIRECTORY_SEPARATOR, $className); 
include_once $file . 'php'; 
} 
$userRegister = new User_Register(); 


Although this method is convenient, when introducing multiple libraries in a large application, it may cause some puzzling problems due to the autoload mechanism of different libraries. After the introduction of the SPL standard library in PHP5, we added a new solution, the spl_autoload_register() function.

The function of this function is the function register to SPL __autoload function in the stack, and remove the system default __autoload () function. Once the spl_autoload_register() function is called, the system calls all the functions registered with the spl_autoload_register() function sequentially when the undefined class is called, instead of calling the function automatically, calling the User/ register.php instead of user_register.class.php:
 
//Don't load I
function __autoload($className) { 
include_once $className . '.class.php'; 
} 
//Load I
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 safer way to initialize the call, as follows:
 
//The system defaults to this function
function __autoload($className) { 
include_once $className . '.class.php'; 
} 
//S. S. S. S. S. S. S
function autoload($className) { 
$file = str_replace('_', DIRECTORY_SEPARATOR, $className); 
include_once $file . '.php'; 
} 
//Accidentally load the wrong function name and cancel the default arbitration mechanism at the same time...  � 
spl_autoload_register('_autoload', false); 
//Fault-tolerant mechanism
if(false === spl_autoload_functions()) { 
if(function_exists('__autoload')) { 
spl_autoload_register('__autoload', false); 
} 
} 

Quirks: in Unix/Linux environments, if you have multiple smaller classes written in a PHP file for administrative convenience, you can quickly distribute copies of different classes by using the ln-s command as a soft link, and then load them using the autoload mechanism.

Related articles: