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 in need can refer to it
When learning the object-oriented nature of PHP, you will know a lot of "syntax sugar", that 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();
Instantiate it here A class A is found, but there is no code related to class A in the code block. Normally, an error should be reported because the corresponding class A is not found. However, if you use the autoload() automatic loading function, the result may not be the same. Same
From the above flow chart:When instantiating a new class on the page, it will first find the corresponding class code in the current directory. If not, go to autoload. The stack looks for the corresponding autoloading function. If there is one, the class will be automatically loaded. If not, an error will be thrown.
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 classes of automatic loading. Therefore, use of the __autoload() function is no longer recommended and may be deprecated in a future version.
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 indentation is used 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 the redefinition of automatic loading, but are there any differences? 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.
Summary: The above is the entire content of this article, I hope it will be helpful to everyone's study.
Related recommendations:
PHP method to implement native DOM object manipulation XML
phpimplementation Refund function of WeChat payment
php Alipay series computer website payment
##
The above is the detailed content of PHP object-oriented automatic loading mechanism principle and usage. For more information, please follow other related articles on the PHP Chinese website!