In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article focuses on "how to use the PHP SPL extension library". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Now let the editor take you to learn how to use the PHP SPL extension library.
1. _ _ autoload
This is an autoload function that is triggered in PHP5 when we instantiate an undefined class. Look at the following example:
. / myClass.php
You can see from the above that these are two files. In the index.php below, there is a myClass class, but obviously this file does not exist. Now the _ _ autoload function will be automatically called, and the "myClass" class name string will be passed directly to _ _ autoload as a parameter. At this time, the file can be introduced inside the auto-loading function, and the class will be initialized normally after introduction. This function was deprecated 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 seems to have the same effect as autoload. But in fact, the function of spl_autoload_register is more powerful, and the parameter of autoload is just a function name, which is dead. It can only be declared once, and once autoload is used, the function cannot be used again.
Please note that there can be only one _ _ autoload in a project. If you encounter two _ _ autoload during the execution of PHP, you will directly report an error.
Obviously, autoload does not meet the requirements, so there is a SPL extension. Spl_autoload_register accepts function names or closures, or arrays as parameters, and the corresponding files can be introduced inside the closures. And spl_autoload_register can register an auto-load queue, first registered, first called.
Parameter autoload_function the autoload function to be registered. If no arguments are provided, the default implementation function spl_autoload () for autoload is automatically registered. Throw this parameter sets whether spl_autoload_register () throws an exception when autoload_function cannot be successfully registered. Prepend if true,spl_autoload_register () adds a function to the beginning of the queue, not the end of the queue.
Can be used in conjunction 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 in 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 class, so function_1 is called automatically and the file is introduced, but there is still no TestClass_2 class in the file, so function_2 is automatically called and the file is introduced. Related other SPL functions
3.1 spl_autoload_call
This function requires the user to display the call to all registered autoload functions. Acting after spl_autoload_register. Just pass in the function name. The file can be introduced manually.
3.2 spl_autoload_functions
You can get all the registered autoload functions, which also function after spl_autoload_register.
3.3 spl_autoload_extensions
Register and return the default file extension used by the spl_autoload function, but this interface and the spl_autoload function are of little use. Spl_autoload is the default implementation of autoload, which means that spl_autoload encapsulates autoload again. By default, this function converts the class name to lowercase, adds the .inc or .php extension to the lowercase class name as the file name, and then checks for the existence of the file in all inclusion paths (include paths).
The _ _ autoload function is used to handle the automatically loaded function. When PHP cannot find the specified class, it will call the autoload class to load the required class. The _ _ autoload is just an abstract definition, and the implementation (the implementation defines how to load, what the loading rules are, what the loaded file is, etc.) is given to the user, while spl_autoload is an implementation of autoload defined by SPL. The loading rule implemented by the spl_autoload function is to go to include paths to find the right class. Spl_autoload follows the loading rules of psr-0, and include paths is the path that is queried when loading. Other self-implemented autoload classes can be registered through spl_autoload_register, and after registration, you can automatically call the registered method to load when the class is needed. Spl_autoload is also an implementation of autoload, which is supposed to be registered, but because it is an internal default implementation, it has been automatically registered in PHP.
Spl_autoload is not very useful now, it should be because the historical problems remain in PHP. At present, most programs do not use spl_autoload to load automatically, because its rules are fixed and are not suitable to derive some functions.
Because PHP has only one automatic loading method, SPL's spl_autoload and spl_autoload_register compete for this method, so there are a lot of compromises in the C implementation of SPL. When you do not use spl_autoload_register to register any custom auto-load function, PHP's auto-load method is hung under spl_autoload, while after spl_autoload_register registers the auto-load function, PHP's auto-load method is hung under spl_autoload_call, and spl_autoload will also become an option to enter spl_autoload_register 's auto-load queue.
At this point, I believe you have a deeper understanding of "the use of the PHP SPL extension library". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.