PHP's spl_autoload standard library method, phpspl_autoload_PHP tutorial

WBOY
Release: 2016-07-13 10:24:02
Original
825 people have browsed it

php's spl_autoload standard library method, phpspl_autoload

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

<?<span>php
    </span><span>function</span> __autoload(<span>$classname</span><span>){
        </span><span>if</span>(<span>is_file</span>(<span>$classname</span>.'.php'<span>){
            </span><span>include</span> <span>$classname</span>.'.php'<span>;
        } </span><span>elseif</span>(<span>is_file</span>(<span>$classname</span>.'.inc'<span>){
            </span><span>include</span> <span>$classname</span>.'.inc'<span>;
        }
    }</span>
Copy after login

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 specific files by registering the extension

<?<span>php
    spl_autoload_extensions(</span>'.php,.inc,.some');
Copy after login

So how to automatically load spl_autoload? The method is

<?<span>php
    spl_autoload_register();</span>
Copy after login

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

<?<span>php
    </span><span>set_include_path</span>(<span>get_include_path</span>() . PATH_SEPARATOR . 'some/path' . DIRECTORY_SEPARATOR);
Copy after login

These methods are commonly used in PHP frameworks.

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

__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

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

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/828048.htmlTechArticlephp’s spl_autoload standard library method, phpspl_autoload The spl_autoload method in php5 is equivalent to implementing your own __autoload? php function __autoload( $classname ){ if ( is_file ( $class...
Related labels:
spl
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