PHP SPL extension library easy to use

Time:2020-2-26

1. __autoload

This is an auto loading function, which is triggered in PHP5 when we instantiate an undefined class. See the following example:

./myClass.php
<?php
class myClass {
    public function __construct() {
        echo "myClass init'ed successfuly!!!";
    }
}
?>
./index.php
<?php
// we've writen this code where we need
function __autoload($classname) {
    $filename = "./". $classname .".php";
    include_once($filename);
}
// we've called a class ***
$obj = new myClass();
?>

It can be seen from the above that these are two files. In index.php below, there is a new MyClass class, but it is obvious that this file does not exist. Now, it will automatically call the autoload function, and directly pass the class name string “MyClass” to autoload as a parameter. At this time, the autoload function can import the file internally, and then initialize the class normally. This function was discarded after PHP 7.2.0.

2. spl_autoload_register

SPL ﹣ autoload ﹣ register can automatically register a function, that is, when a class that does not exist is accessed in the PHP file, it will automatically call the function, and then execute the function inside the function, which looks likeAutoload works the same. But in fact, the SPL autoload register function is more powerful,The autoload parameter is just a function name, which is fixed. It can only be declared once. After autoload is used, the function cannot be used again.

Please note: there can only be one autoload in a project. If two autoloads are encountered during PHP execution, an error will be reported directly.

Obviously,Autoload cannot meet the requirements, so there is an SPL extension. SPL autoload register accepts function names or closures, or arrays as parameters. In the closure, you can import the corresponding files. And SPL autoload register can register an autoload queue, which is registered first and called first.

parameter 
autoload_function
欲注册的自动装载函数。如果没有提供任何parameter,则自动注册 autoload 的默认实现函数spl_autoload()。
throw
此parameter设置了 autoload_function 无法成功注册时, spl_autoload_register()是否抛出异常。
prepend
If true, SPL autoload register() adds a function to the top of the queue, not the end of the queue.

It can be used in combination with require once. Such as:

function_1(){
     $clsName = str_replace("\",DIRECTORY_SEPARATOR, $class_name);
    if (is_file(__DIR__.DIRECTORY_SEPARATOR."src".DIRECTORY_SEPARATOR.$clsName . '.php')) {
        //There is a class named testclass? 1 inside the file
        require_once(__DIR__.DIRECTORY_SEPARATOR."src".DIRECTORY_SEPARATOR.$clsName.'.php'); 
    }
}
function_2(){
     $clsName = str_replace("\",DIRECTORY_SEPARATOR, $class_name);
    if (is_file(__DIR__.DIRECTORY_SEPARATOR."Module".DIRECTORY_SEPARATOR.$clsName . '.php')) {
        //There is a class named testclass? 2 inside the file
        require_once(__DIR__.DIRECTORY_SEPARATOR."Module".DIRECTORY_SEPARATOR.$clsName.'.php');
    }
}
spl_autoload_register('function_1');
spl_autoload_register('function_2');
$obj = new testclass? 2(); // Currently, there is no testclass? 2, so function? 1 is called automatically, and the file is imported. However, there is no testclass? 2 in the imported file, so function? 2 is called automatically, and the file is imported. At this time, initialization is normal

3. Related other SPL functions

PHP SPL extension library easy to use

3.1 spl_autoload_call

This function requires the user to display and call all registered autoload functions. After SPL autoload register. Just pass in the function name. You can import the file manually.

3.2 spl_autoload_functions

You can get all the registered autoload functions, which are also used after SPL autoload register.

3.3 spl_autoload_extensions

Register and return the default file extension used by SPL autoload function, but this interface and SPL autoload function are not useful. SPL autoload is the default implementation of autoload, which means that SPL autoload encapsulates autoload again. By default, this function first converts the class name to lowercase, then adds the. Inc or. PHP extension to the lowercase class name as the file name, and then checks whether the file exists in all include paths.

__Autoload function is used to handle the automatic loading function. When PHP cannot find the specified class, it will call the automatic loading class to load the required class.
__Autoload is only an abstract definition. The implementation (implementation is to define how to load, what are the loading rules, what are the loaded files, etc.) is given to the user. Spl_autoload is an implementation of autoload defined by SPL. The loading rule implemented by SPL ﹣ autoload function is to find the right class in include paths. SPL autoload follows the loading rule of psr-0, and include paths is the path to be queried when loading.
Other autoload classes that you have implemented can be registered through SPL autoload register. After registration, you can automatically call the registered method to load when you need the class. SPL ﹣ autoload is also an implementation of autoload, and it is also required to register. It is only because it is the internal default implementation, all of which have been automatically registered in PHP.

Nowadays, SPL autoload is not very useful. It should be because historical problems remain in PHP. At present, most programs do not use SPL autoload to do automatic loading, because its rules are fixed and it is not suitable for deriving some functions.

Because PHP has only one automatic loading method, SPL autoload and SPL autoload register of SPL need to compete for this method, so in the C implementation of SPL, there are many compromise methods. When you do not use SPL autoload register to register any custom autoload function, PHP’s autoload method is hung under SPL autoload. After SPL autoload register registers the autoload function, PHP’s autoload method is hung under SPL autoload call, and SPL autoload will become a standby option to enter SPL autoload register The autoload queue for.

Recommended Today

PHP Basics – String Array Operations

In our daily work, we often need to deal with some strings or arrays. Today, we have time to sort them out String operation <?php //String truncation $str = ‘Hello World!’ Substr ($STR, 0,5); // return ‘hello’ //Chinese string truncation $STR = ‘Hello, Shenzhen’; $result = mb_ Substr ($STR, 0,2); // Hello //First occurrence of […]