Home  >  Article  >  Backend Development  >  Analysis of the difference between spl_autoload_register() and __autoload() in PHP_PHP Tutorial

Analysis of the difference between spl_autoload_register() and __autoload() in PHP_PHP Tutorial

WBOY
WBOYOriginal
2016-07-13 10:30:01756browse

Regarding spl_autoload_register() and __autoload(), I believe most people will choose the former? Look at the usage of both:

Copy code The code is as follows:

//__autoload usage
function __autoload($classname)
{
$filename = "./class/".$classname.".class.php";
if (is_file($filename))
{
include $filename;
}
}

//spl_autoload_register usage
spl_autoload_register('load_class');

function load_class($classname)
{
$filename = "./class/" .$classname.".class.php";
if (is_file($filename))
{
include $filename;
}
}

The benefits of using spl_autoload_register() are indescribable:
(1) Automatically loading objects is more convenient, and many frameworks do this:

Copy code The code is as follows:

class ClassAutoloader {
public function __construct() {
spl_autoload_register(array ($this, 'loader'));
}
private function loader($className) {
echo 'Trying to load ', $className, ' via ', __METHOD__, "()n";
                       include $className . '.php'; >$obj = new Class2();



(2) You need to know that the __autoload() function can only exist once. Of course, spl_autoload_register() can register multiple functions



Copy code

The code is as follows:

function a () { include 'a.php'; } function b () { include 'b.php';
}
spl_autoload_register('a');
spl_autoload_register('b');



(3) SPL functions are rich and provide more functions, such as spl_autoload_unregister() to unregister registered functions, spl_autoload_functions() to return all registered functions, etc.



For details, please refer to the PHP Reference Manual: About the SPL function list.

Note:

If it has been implemented in your program_ _autoload function, it must be explicitly registered in the __autoload stack. Because the Analysis of the difference between spl_autoload_register() and __autoload() in PHP_PHP Tutorialspl_autoload_register() function will replace the __autoload function in Zend Engine with spl_autoload() or spl_autoload_call()




Copy code

Code As follows:

/*** The __autoload method will become invalid after spl_autoload_register, because the autoload_func function pointer already points to the spl_autoload method * You can add the _autoload method to the autoload_functions list through the following method */ spl_autoload_register( '__autoload' );





http://www.bkjia.com/PHPjc/768124.html
www.bkjia.comtrue

http: //www.bkjia.com/PHPjc/768124.htmlTechArticle Regarding spl_autoload_register() and __autoload(), I believe most people will choose the former? Look at the usage of the two: Copy the code The code is as follows: //__autoload usage function __autoload($clas...
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