Home  >  Article  >  Backend Development  >  Detailed explanation of the difference between spl_autoload_register and autoload_PHP tutorial

Detailed explanation of the difference between spl_autoload_register and autoload_PHP tutorial

WBOY
WBOYOriginal
2016-07-21 15:09:30810browse

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 autoloading 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.
Example
Suppose we have a class file A.php, which defines a class named A:

Copy code The code is as follows:

class A
{
public function __construct()
{
echo 'Got it.';
}
}

Then we have an index.php that needs to use this class A. The conventional writing method is
Copy code The code is as follows:

require('A.php');
$a = new A();

But one problem is that if our index.php needs to contain not just class A, but many classes, then we must write many lines of require statements, which sometimes makes people feel uncomfortable.
But in versions after php5, we no longer need to do this. In php5, the autoload function is automatically called when trying to use a class that has not been defined, so we can write the __autoload function to let php automatically load the class without having to write a long list of include files.
For example, in the above example, index.php can be written like this:
Copy the code The code is as follows:

function __autoload($class)
{
$file = $class . '.php';
if (is_file($file)) {
require_once($file);
}
}
$a = new A();

Of course the above is just the simplest demonstration, __autoload just goes to include_path to find class files And load, we can define the rules of __autoload loading classes according to our own needs.
In addition, if we do not want to call __autoload during automatic 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:
Copy Code The code is as follows:

function loader($class)
{
$file = $class . '.php';
if (is_file($file)) {
require_once($file);
}
}
spl_autoload_register('loader');
$a = new A();

This can also run normally. At this time, when PHP is looking for a class, it does not call __autoload but calls our own defined function loader. For the same reason, the following writing method is also possible:
Copy the code The code is as follows:

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();

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/327279.htmlTechArticlespl_autoload_register (PHP 5 = 5.1.2) spl_autoload_register — Register __autoload() function description bool spl_autoload_register ([ callback $ autoload_function ] ) Register the function to SPL...
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