Home  >  Article  >  Backend Development  >  PHP function spl_autoload_register() usage and __autoload() introduction_PHP tutorial

PHP function spl_autoload_register() usage and __autoload() introduction_PHP tutorial

WBOY
WBOYOriginal
2016-07-21 15:20:44844browse

I won’t talk about the usage of __autoload() anymore, I have mentioned it before in my WEB development notes. How to use PHP __autoload function (automatically load class files), original address: http://www.jb51.net/article/29625.htm.
Let’s talk about the usage of spl_autoload_register(). It’s very simple. It can be understood like this, which is to declare a custom __autoload(). It can be function A or function B. You can do whatever you want. Of course, the function body must be written in the same way as __autoload().
This method will be called when PHP cannot find the class file. When registering its own function or method, PHP will not call the __autoload() function, but will call the custom function
spl_autoload_register('func_name' ; () Function
Description
bool spl_autoload_register ([ callback $autoload_function ] )
Register the function into the SPL __autoload function stack. Activate functions in this stack if they are not already active.
If the __autoload function has been implemented in your program, it must be explicitly registered in the __autoload stack. Because the
spl_autoload_register() function will replace the __autoload function in Zend Engine with spl_autoload() or
spl_autoload_call().
Parameters
autoload_function
The autoload function to be registered. If no parameters are provided, the default implementation function
spl_autoload() of autoload is automatically registered.
Return value
Returns TRUE if successful, FALSE if failed.
Note: SPL is the abbreviation of Standard PHP Library. It is an extension library introduced in PHP5. Its main functions include the implementation of the autoload mechanism and various Iterator interfaces or classes. The SPL autoload mechanism is implemented by pointing the function pointer autoload_func to a self-implemented function with autoloading function. SPL has two different functions spl_autoload and spl_autoload_call. Different automatic loading mechanisms are implemented by pointing autoload_func to these two different function addresses.
The code is as follows:
test.class.php



Copy code

The code is as follows:

class abc{ function __construct()
{
echo 'www.chhua.com;
}
}
?>


load.php


Copy code

The code is as follows:
class LOAD {
static function loadClass($class_name)
{
$filename = $class_name.".class.php";
if (is_file($filename)) return include_once $filename;
}
}
/**
* Set the automatic loading of objects
* spl_autoload_register — Register given function as __autoload() implementation
*/
spl_autoload_register(array('LOAD', 'loadClass'));
$a = new Test();//Achieve automatic loading, many frameworks Use this method to automatically load classes
?>





http://www.bkjia.com/PHPjc/325046.html
www.bkjia.com

truehttp: //www.bkjia.com/PHPjc/325046.htmlTechArticleThe usage of __autoload() will not be mentioned anymore. It has been mentioned in my WEB development notes before. How to use PHP __autoload function (automatically load class files), original address: http://www.jb...
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