Detailed Explanation of spl_autoload in php

  • 2021-07-22 09:09:29
  • OfStack

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

spl_autoload is the default auto-load function implemented by SPL, and its function is relatively simple. It can take two parameters, the first parameter is $class_name, which represents the class name, and the second parameter, $file_extensions, which is optional, represents the extension of the class file "title=" extension. " > Extensions, you can specify multiple extensions in $file_extensions "title=" Extensions " > Extensions and exhibition names can be separated by semicolons; If not specified, it will use the default extension "title=" Extension " > Extension. inc or. php. spl_autoload first turns $class_name to lowercase, then searches all include path for the $class_name. inc or $class_name. php file (if the $file_extensions parameter is not specified), and if found, loads the file. You can manually use spl_autoload ("Person", ". class. php") to load the Person class. Actually, it is similar to require/include, except that it can specify multiple extensions "title=" Extensions. " > 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. You can point autoload_func to spl_autoload by calling spl_autoload_register () for the first time in the PHP script without using any parameters.

From the above explanation, we know that the function of spl_autoload is relatively simple, and it is implemented in SPL extension, so we cannot extend its function. What if you want to implement your own more flexible automatic loading mechanism? At this point, the spl_autoload_call function comes out.

Let's first look at the wonders of the implementation of spl_autoload_call. In the SPL module, there is a global variable autoload_functions, which is essentially an HashTable, but we can simply regard it as a linked list, and every element in the linked list is a function pointer, pointing to a function with the function of automatically loading classes. The implementation of spl_autoload_call itself is very simple, just simply execute each function in this linked list in sequence, judge whether the required class has been loaded once after each function is executed, and return directly if the load is successful, and no longer continue to execute other functions in the linked list. If the class is not loaded after all the functions in this list have been executed, spl_autoload_call exits without reporting an error to the user. Therefore, using autoload mechanism, there is no guarantee that the class will be automatically loaded correctly, the key is to see how your automatic loading function is implemented.

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


<?php
    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 registry directory. Of course, you can also specify specific types of files by registering extensions


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

In this way, it also searches for the. some file. By default, php does not start spl_autoload, so how do you automatically make spl_autoload work? The method is


<?php
    spl_autoload_register();

spl_autoload_register has a $callback parameter, which automatically registers spl_autoload if it is not specified. In order to search for more auto-load directories, you can set auto-load directories in front of these codes


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

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

These methods are commonly used in php framework. For example, connect the above introductions in series:


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

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


<?php
    ClassB extends ClassA {
        // code..
    }     $a = new ClassA;
    $b = new ClassB;


Related articles: