1. __autoload
This is an autoload function that in PHP5 is triggered when we instantiate an undefined class. Look at 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(); ? >Copy the code
Autoload: myClass = myClass; autoload: myClass = myClass; The file is then imported into the autoload function, and the class is initialized normally. This function was deprecated in PHP 7.2.0.
2. splautoloadregister
Splautoloadregister automatically registers functions, that is, when a non-existent class is accessed from within a PHP file, the function is automatically called and then executed, seemingly in the same way that Autoload does. However, splautoloadRegister is a more powerful function. The parameter of autoload is just a function name, which is determinate. It can only be declared once. Once autoload is used, it cannot be used again.
Please note that there can only be one Autoload in a project, and an error will be reported if two Autoloads are encountered during PHP execution.
Obviously, autoload doesn’t fit the bill, so we have an SPL extension. Splautoloadregister takes a function name or a closure, or an array, as an argument. Inside the closure, we can import the corresponding file. And splautoloadRegister can register an auto-loading queue, first registered, first called.
Parameter autoload_function Automatic loading function to register. If no arguments are provided, the default implementation function spl_autoload() automatically registers autoload. Throw This parameter sets whether spl_autoload_register() throws an exception if autoload_function fails to register successfully. Prepend if true, spl_autoload_register() adds functions to the head of the queue, not to the end.Copy the code
It 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')) {// The file has an internal class named TestClass_1 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')) {// The file has an internal class named TestClass_2 require_once(__DIR__.DIRECTORY_SEPARATOR."Module".DIRECTORY_SEPARATOR.$clsName.'.php'); } } spl_autoload_register('function_1'); spl_autoload_register('function_2'); $obj = new TestClass_2(); // There is no TestClass_2 class, so we automatically call function_1 and import file, but the imported file still does not have TestClass_2 class, so we automatically call function_2 and import file, and initialize normallyCopy the code
3. Other related SPL functions
3.1 SPLautoloadcall
This function requires the user to explicitly call all registered Autoload functions. Function after splautoloadRegister. Just pass in the function name. You can import files manually.
3.2 SPLautoloadfunctions
All of the autoload functions that have been registered are available after the SplautoloadRegister.
3.3 SPLautoloadextensions
Register and return the default file extension used by the splautoload function, but this interface and the splautoload function are of little use. Splautoload is the default implementation of Autoload, which means splautoload wraps Autoload again. By default, this function converts the class name to lower case and adds the extension of.inc or.php to the lower case name. Then check for the file in all include Paths.
The __autoload function is used to handle auto-loading, which is called when PHP cannot find the specified class and loads the required class. __autoload is just an abstract definition, the implementation (which defines how toload, what rules toload, what files toload, etc.) is handed to the user, while spl_autoload is an implementation of the AUtoload defined by SPL. The loading rule implemented by the spl_autoload function is to look for the appropriate class in include Paths. Spl_autoload follows the pSR-0 load rule, and include Paths are the paths to be queried at load time. Other self-implemented Autoload classes can be registered through splautoloadRegister, which automatically calls the registered methods toload classes when needed. Spl_autoload is also an implementation of Autoload, which should be registered, but since it is the internal default implementation, it is automatically registered in PHP.
Splautoload doesn’t have much use today, probably due to historical issues in PHP. Most programs don’t use Splautoload for auto-loading, because its rules are already dead and it’s not suitable for generating functionality.
Because PHP has only one auto-load method, SPL splautoload and SplautoloadRegister compete for this method, so there are many compromises in SPL’s C implementation. When splautoloadRegister is not used to register any custom autoloading functions, PHP autoloading methods are attached to splautoload, and when splautoloadRegister registers autoloading functions, The PHP autoload method is attached to the splautoloadCall method, and splautoload becomes an alternative to the splautoload_register autoload queue.