Home >Backend Development >PHP Tutorial >Use of SPL spl_autoload_register and __autoload methods in php_PHP tutorial
In PHP, the spl_autoload_register and __autoload methods are only available in PHP5. Now I will introduce to you how to use these two magic functions. You can learn more about them.
The spl_autoload_register() function should be one of the most used and core functions in mainstream frameworks. It can automatically register functions and classes, implement functions similar to __autoload(), simplify the calling and loading of classes, and improve work efficiency. efficiency.
Supported version: PHP 5 >= 5.1.2
As for efficiency issues. The PHP manual says this:
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().
spl_autoload_register
(PHP 5 >= 5.1.2)
spl_autoload_register — register __autoload() 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 of autoload is automatically registered
spl_autoload().
Return value
Returns TRUE on success, FALSE on failure.
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 | Copy code | ||||
|
Under the supplement:
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
spl_autoload_register( '__autoload' );
Also we can use our custom loading method:
The first functional formula:
The code is as follows | Copy code | ||||||||||||
{ $class_file = strtolower($classname).".php";
require_once($class_file); }
|
The code is as follows | Copy code |
class Loader { Public static function my_own_loader($classname) { $class_file = strtolower($classname).".php"; If (file_exists($class_file)){ require_once($class_file); } } } // Pass the names of classes and methods in the form of arrays spl_autoload_register(array("Loader","my_own_loader")); $a = new A(); |
The code is as follows | Copy code |
static public function myAutoload($class){ if(file_exists(APPPATH.'models'.DIRECATORY_SEPARATOR.$class.'.php')){ require_once APPPATH.'models'.DIRECATORY_SEPARATOR.$class.'.php'; } } /** * Register loader*/ static public function autoload(){ spl_autoload_register(array(__CLASS__, 'myAutoload')); If(class_exists('__autoload')){ spl_autoload_register('__autoload'); } } MY_Controller::autoload(); |
Of course, the above is just the simplest demonstration. __autoload just goes to include_path to find the class file and loads it. We can define the rules for __autoload to load classes according to our own needs.
In addition, if we do not want to call __autoload when automatically loading, but call our own function (or class method), we can use spl_autoload_register to register our own autoload function. Its function prototype is as follows:
bool spl_autoload_register ([callback $autoload_function] )
Let’s continue to rewrite the above example:
The code is as follows | Copy code | ||||||||
function loader($class) {
if (is_file($file)) { require_once($file); } spl_autoload_register('loader');$a = new A();
|
The code is as follows | Copy code |
view plaincopy to clipboardprint? class Loader { public static function loadClass($class) { $file = $class . '.php'; if (is_file($file)) { require_once($file); } } } spl_autoload_register(array('Loader', 'loadClass')); $a = new A(); |