Home  >  Article  >  Backend Development  >  Reason for failure of PHP function __autoload (related to smarty)

Reason for failure of PHP function __autoload (related to smarty)

WBOY
WBOYOriginal
2016-07-29 09:16:121053browse

PHP function __autoload can achieve simple automatic loading, but after introducing smarty, it was found that the __autoload function was invalid. Later, it was found 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 is not executed. The official website’s analysis is: If the __autoload() function has been implemented in your program, it must be explicitly registered to __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();

Copyright Statement: This article is an original article by the blogger and may not be reproduced without the blogger's permission.

The above introduces the reasons for the failure of PHP function __autoload (related to smarty), including the relevant content. I hope it will be helpful to friends who are interested in PHP tutorials.

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