Home > Backend Development > PHP Tutorial > Detailed explanation of spl_autoload in php_PHP tutorial

Detailed explanation of spl_autoload in php_PHP tutorial

WBOY
Release: 2016-07-13 10:16:44
Original
897 people have browsed it

Detailed explanation of spl_autoload in php

spl_autoload is the default automatic loading function implemented by SPL, and its function is relatively simple. It can receive two parameters. The first parameter is $class_name, which represents the class name. The second parameter $file_extensions is optional and represents the extension name of the class file. " title="Extension name"> extension name, which can be in Specify multiple extensions in $file_extensions" title="Extension">Extension names, just separate them with semicolons; if not specified, it will use the default extension" title="Extension name"> extension .inc or .php. spl_autoload first changes $class_name to lowercase, and then searches for $class_name.inc or $class_name.php files in all include paths (if the $file_extensions parameter is not specified), if If found, load the class file. You can manually use spl_autoload("Person", ".class.php") to load the Person class. In fact, it is similar to require/include, except that it can specify multiple extensions. title="Extension">Extension.

How to make spl_autoload work automatically, that is, point autoload_func to spl_autoload? The answer is to use the spl_autoload_register function. By calling spl_autoload_register() for the first time in a PHP script without any parameters, you can point autoload_func to spl_autoload.

From the above description, we know that the function of spl_autoload is relatively simple, and it is implemented in the SPL extension, and we cannot expand its function. What if you want to implement your own more flexible automatic loading mechanism? At this time, the spl_autoload_call function makes its debut.

Let’s first take a look at the wonderful features of spl_autoload_call implementation. Inside the SPL module, there is a global variable autoload_functions, which is essentially a HashTable, but we can simply think of it as a linked list. Each element in the linked list is a function pointer, pointing to a function that has the function of automatically loading classes. function. The implementation of spl_autoload_call itself is very simple. It simply executes each function in the linked list in order. After each function is executed, it is judged whether the required class has been loaded. If the loading is successful, it returns directly and does not continue to execute the linked list. other functions. If the class has not been loaded after all functions in this linked list have been executed, spl_autoload_call will exit directly without reporting an error to the user. Therefore, using the autoload mechanism does not guarantee that the class will be automatically loaded correctly. The key still depends on how your autoloading function is implemented.

The standard library method spl_autoload in php5 is equivalent to implementing your own __autoload

The code is as follows:

Function __autoload($classname){
If(is_file($classname.'.php'){
                    include $classname.'.php';
           } elseif(is_file($classname.'.inc'){
                    include $classname.'.inc';
}
}

It will automatically look for the .php/.inc file with the same name as $classname in the registration directory. Of course, you can also specify a specific type of file by registering the extension

The code is as follows:

spl_autoload_extensions('.php,.inc,.some');

This way, it will also search for .some files. By default, php will not start spl_autoload, so how can we automatically make spl_autoload take effect? The method is

The code is as follows:

spl_autoload_register();

spl_autoload_register has a $callback parameter. If not specified, it will automatically register spl_autoload. In order to search for more autoloading directories, you can set the autoloading directory in front of these codes

The code is as follows:

set_include_path(get_include_path() . PATH_SEPARATOR . 'some/path' . DIRECTORY_SEPARATOR);

In this way, when php cannot find the specified class, it will search in the directory specified by set_include_path.

These methods are commonly used in PHP frameworks. For example, connect the above introductions together:

The code is as follows:

set_include_path(get_include_path() . PATH_SEPARATOR . 'some/path' . DIRECTORY_SEPARATOR);
spl_autoload_extensions('.php,.inc,.some');
spl_autoload_register();

When you want to load the classA class under some/path, it will look for classa.php or classa.inc or classa.some in the directory, so you can safely use new classA or extends classA

The code is as follows:

ClassB extends ClassA {
// code..
}

$a = new ClassA;
$b = new ClassB;

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/896774.htmlTechArticleDetailed explanation of spl_autoload in php spl_autoload is the default automatic loading function implemented by SPL, and its function is relatively simple. It can receive two parameters. The first parameter is $class_name, which represents the class name...
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template