Home > Backend Development > PHP Tutorial > Detailed explanation of spl_autoload in php, detailed explanation of spl_autoload_PHP tutorial

Detailed explanation of spl_autoload in php, detailed explanation of spl_autoload_PHP tutorial

WBOY
Release: 2016-07-13 10:16:57
Original
846 people have browsed it

Detailed explanation of spl_autoload in php, detailed explanation of spl_autoload

SPL has two different functions spl_autoload and spl_autoload_call. Different automatic loading mechanisms are implemented by pointing autoload_func to these two different function addresses.

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

Copy code 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

Copy code 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

Copy code 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

Copy code 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:

Copy code 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

Copy code The code is as follows:

ClassB extends ClassA {
// code..
}

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

Teach the usage of spl_autoload_register and spl_autoload_unregister in PHP

This is a PHP function similar to autoloading, such as __autoload, but this can only pass in our NEW class name. If you want to call your own defined function during NEW, you can use
spl_autoload_register

Urgent, could you please explain the __autoload function and spl_autoload_register() function in PHP? I looked for information on the Internet and couldn’t understand it.

__autoload is commonly used in automatic loading of class libraries
This is the method mentioned on the Internet. According to the class name, find the class file, and then require_one
spl_autoload_register()

The biggest flaw of __autoload is that it cannot Multiple autoload methods

Okay, think about the following scenario. Your project references a project of someone else. There is an __autoload in your project, and another person’s project also has an __autoload, so there are two __autoload conflicts. The solution is to modify __autoload to become one, which is undoubtedly very cumbersome.

Therefore, we urgently need to use an autoload call stack, so that the autoload series functions of spl appear. You can use spl_autoload_register to register multiple custom autoload functions

If your PHP version is greater than 5.1, you can use spl_autoload

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/895115.htmlTechArticleDetailed explanation of spl_autoload in php, detailed explanation of spl_autoload. SPL has two different functions, spl_autoload, spl_autoload_call. By pointing autoload_func to these two Different function addresses to implement different...
Related labels:
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