Home > Article > Backend Development > Detailed explanation of PHP automatic loading mechanism definition and usage examples
This article mainly introduces the principle and usage of the PHP object-oriented automatic loading mechanism. It analyzes the principles, related functions and precautions of the PHP object-oriented automatic loading mechanism in the form of examples. , Friends who need it can refer to
The examples in this article describe the principles and usage of PHP's object-oriented automatic loading mechanism. Share it with everyone for your reference, the details are as follows:
When learning PHP's object-oriented approach, you will know a lot of "syntax sugar", which is magic methods. There is a magic method to add automatic loading, called: autoload();
Look at a piece of code first
<?php function autoload($classname) { $filename = "./". $classname .".php"; include_once($filename); } new a();
A class A is instantiated here, but there is no reference to class A in the code block The code, according to common sense, should report an error because the corresponding class A is not found, but if you use the autoload() automatic loading function, the result may be different
#From the flow chart above: When instantiating a new class on the page, it will first find the corresponding class code in the current directory. If not, go to the autoload stack to find the corresponding automatic loading function. If there is, it will automatically load it. This class will throw an error if there are no words.
This is a mechanism for PHP to automatically load. Then the focus is on the back. What if I have multiple automatically loaded functions!
PHP provides an SPL function
spl_autoload_register(); // 注册autoload函数
Official: spl_autoload_register() provides a more flexible way to implement automatic loading of classes. Therefore, use of the autoload() function is no longer recommended and may be deprecated in a future release.
However, this function is used in both PHPexecl and PHPWord for automatic loading, but there is a difference between the two! !
PHPexecl automatic loading method (The author here is probably a Python engineer, otherwise there are no curly braces, and use indentation to indicate )
public static function Register() { $functions = spl_autoload_functions(); foreach ( $functions as $function) spl_autoload_unregister($function); $functions = array_merge(array(array('PHPExcel_Autoloader','Load')),$functions); foreach ( $functions as $function) $x = spl_autoload_register($function); return $x; }
PHPWord automatic loading method
public static function Register() { return spl_autoload_register(array('PHPWord_Autoloader', 'Load')); }
Both of these two methods can complete redefinition of automatic loading, but what is the difference? If the code is run independently, both cases can be run, but if it is integrated into a framework, such as the YII framework. Then PHPWord's automatic loading is invalid.
Because the YII framework automatically comes with an autoloading function, and it is already registered when the code is running, and spl_autoload_register() will load the new autoloading function at the back of the autoload queue. When all PHPWord is running, it
calls the automatic loading mechanism defined by the YII framework, and it is not the loading method of PHPWord.
So if you look at the loading function of PHPexecl, you will understand.
The above is the detailed content of Detailed explanation of PHP automatic loading mechanism definition and usage examples. For more information, please follow other related articles on the PHP Chinese website!