Home  >  Article  >  Backend Development  >  Detailed explanation of usage examples of PHP's autoload automatic loading mechanism

Detailed explanation of usage examples of PHP's autoload automatic loading mechanism

伊谢尔伦
伊谢尔伦Original
2017-07-01 09:33:351449browse

Since PHP5, the autoload interceptor method has been introduced, which can automatically include class files Reference. See the specific usage below.

During the PHP development process, if you want to introduce a class from the outside, you usually use the include and require methods to include the file that defines this class. However, this may result in a large number of include or require method calls in the new script that references the file. If accidentally omitted, errors will occur, making the code difficult to maintain.

Since PHP5, the autoload interceptor method has been introduced, which can automatically include references to class files. Usually we will write like this:

function autoload($className) { 
include_once $className . '.class.php'; 
} 
$user = new User();

When the PHP engine tries to instantiate an unknown class During operation, the autoload() method will be called, giving PHP the last chance to load the required classes before an error occurs. Therefore, when the above code is executed, the PHP engine actually automatically executes the autoload method for us and includes the file User.class.php.

Exceptions thrown in the autoload function cannot be caught by the catch statement block and result in a fatal error.

If you use PHP's CLI interactive mode, the automatic loading mechanism will not be executed.

When you want to use PEAR style naming rules, for example, you need to introduce the User/Register.php file, you can also implement it like this:

//加载我 
function autoload($className) { 
$file = str_replace('_', DIRECTORY_SEPARATOR, $className); 
include_once $file . 'php'; 
} 
$userRegister = new User_Register();

Although this method is convenient, it will not work in a large application. If multiple class libraries are introduced, some inexplicable problems may occur due to the autoload mechanisms of different class libraries. After the SPL standard library was introduced in PHP5, we have a new solution, the spl_autoload_register() function.

The function of this function is to register the function into the autoload function stack of SPL and remove the system default autoload() function. Once the spl_autoload_register() function is called, when an undefined class is called, the system will call 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 User_Register.class.php:

Copy code The code is as follows:

//不加载我 
function autoload($className) { 
include_once $className . '.class.php'; 
} 
//加载我 
function autoload($className) { 
$file = str_replace('/', DIRECTORY_SEPARATOR, $className); 
include_once $file . '.php'; 
} 
//开始加载 
spl_autoload_register('autoload'); 
$userRegister = new User_Register();

When using spl_autoload_register(), we can also consider using a safer The initialization calling method is as follows:

The code is as follows:

//系统默认autoload函数 
function autoload($className) { 
include_once $className . '.class.php'; 
} 
//可供SPL加载的autoload函数 
function autoload($className) { 
$file = str_replace('_', DIRECTORY_SEPARATOR, $className); 
include_once $file . '.php'; 
} 
//不小心加载错了函数名,同时又把默认autoload机制给取消了……囧 
spl_autoload_register('_autoload', false); 
//容错机制 
if(false === spl_autoload_functions()) { 
if(function_exists('autoload')) { 
spl_autoload_register('autoload', false); 
} 
}

In a Unix/Linux environment, if you have multiple smaller classes, for the convenience of management, all When written in a php file, it can be quickly distributed into multiple copies with different class names by making soft links with the ln -s command, and then loaded through the autoload mechanism.

The above is the detailed content of Detailed explanation of usage examples of PHP's autoload automatic loading mechanism. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn