What to do if php __autoload fails

藏色散人
Release: 2023-03-12 14:32:01
Original
2428 people have browsed it

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();
Copy after login

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();
Copy after login

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!

Related labels:
php
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template