Home > Article > Backend Development > Introduction to PHP automatic loading mechanism - spl_autoload_register() function, automatic loading of PHP classes
* The functions of include and require are the same. The difference is that include will only generate a warning when an error occurs, while require will throw an error to terminate the script.
* The only difference between include_once and include is that include_once will check whether the file has been introduced, and if so, it will not be introduced again.
spl_autoload_register() function is an important method to realize the function of automatically loading undefined classes. The so-called automatic loading means that when we create a new class, we must first include or require the class file. If there is no include or require , an error will be reported. Then we have to write many include or require files in the header of the file, which is very troublesome.
In order to create a new class normally when there is no include or require class, we have the concept of automatic loading. That is to say, a new class can be new normally without including the class file in advance, so that our file header does not need to contain many include(require). In fact, this is a kind of encapsulation!
The spl_autoload_register function can be used to achieve the above functions. Let’s take a look at the implementation principle.
The parameters of this function are as follows:
The first parameter: autoload_function
This is A function [method] name, which can be a string or array (used to call class methods). The function of this function (method) is to include (requeire) the class file that needs new, so that the file will not be found when new is used. In fact, it encapsulates the include and require functions of the entire project.
Second parameter: throw
This parameter sets whether spl_autoload_register() throws an exception when the autoload_function cannot be successfully registered.
The third parameter: prepend
If it is true, spl_autoload_register() will add the function to the head of the queue instead of the tail of the queue.
When we create a new class and the class file is not included, this autoload_function method will be executed.
Let’s look at an error example first:
<?php //当我们直接new一个未包含class类文件时候会报错 $objDemo = new AutoloadClass();
Correct Using the spl_autoload_register() function
We can see from the following example that when new is an uncontained class, the function of the first parameter function name of spl_autoload_register will be executed. This function has one parameter. It requires the class name of new. The function of this function is to include this class (the class name is consistent with the file name), thus realizing the automatic loading function. That's the principle, it's not very complicated.
<?php // 定义工具类在服务器位置 常量 define('TOOLS_ROOT', __DIR__ . '/'); //文件 autoloadClass.php ,需要new的文件 class AutoloadClass{ public function __construct() { // echo '你已经包含我了'; } } //文件autoloadDemo.php文件 spl_autoload_register('myAutoLoad', true, true); function myAutoLoad($className){ $classFileName = TOOLS_ROOT."{$className}.php"; include $classFileName; }
In addition, we can change it to an anonymous function to achieve:
<?php // 定义工具类在服务器位置 常量 define('TOOLS_ROOT', __DIR__ . '/'); //文件 autoloadClass.php ,需要new的文件 class AutoloadClass{ public function __construct() { // echo '你已经包含我了'; } } spl_autoload_register(function ($className) { $classFileName = TOOLS_ROOT."{$className}.php"; include $classFileName; }, true, true); $objDemo = new AutoloadClass();
For more PHP related knowledge, please visit PHP Chinese website!
The above is the detailed content of Introduction to PHP automatic loading mechanism - spl_autoload_register() function, automatic loading of PHP classes. For more information, please follow other related articles on the PHP Chinese website!