php __autoload fails because the spl_autoload_register function replaces the __autoload function in Zend Engine with spl_autoload. The solution is to re-register the __autoload function.
The operating environment of this article: windows7 system, PHP7.1 version, DELL G3 computer
What should I do if php __autoload fails?
The reason why PHP function __autoload fails (related to smarty)
PHP function __autoload can achieve simple automatic loading, but After introducing smarty, I found that the __autoload function was invalid. Later, I found out that the reason was the spl_autoload_register function.
Execute the following code:
function __autoload($name) { require 'class/'.$name.'.php'; echo '1'; } function autoload_test($name) { echo '2'; } spl_autoload_register('autoload_test'); $ca=new Ca();
The result is output 2. You can see that the __autoload function has not been executed. The official website’s analysis is: If the __autoload() function has been implemented in your program , it must be explicitly registered to the __autoload() queue. Because the spl_autoload_register() function will replace the __autoload() function in Zend Engine with spl_autoload() or spl_autoload_call().
In order for the code to work properly, the __autoload function should be re-registered:
function __autoload($name) { require 'class/'.$name.'.php'; echo '1'; } function autoload_test($name) { echo '2'; } spl_autoload_register('autoload_test'); spl_autoload_register('__autoload'); $ca=new Ca();
Recommended learning: "PHP Video Tutorial"
The above is the detailed content of What to do if php __autoload fails. For more information, please follow other related articles on the PHP Chinese website!