Home >Backend Development >PHP Problem >What to do if php __autoload fails

What to do if php __autoload fails

藏色散人
藏色散人Original
2021-09-14 10:43:192485browse

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.

What to do if php __autoload fails

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn