Details of the differences between spl_autoload_register and autoload

  • 2020-06-03 06:04:13
  • OfStack

spl_autoload_register
(PHP 5 > = 5.1.2)
spl_autoload_register -- Register the ___ 9en () function
instructions
bool spl_autoload_register ([ callback $autoload_function ] )
Register the functions with the SPL ___ function stack. If the functions in the stack are not already active, activate them.
If you have implemented the ___ 17en function in your program, it must be explicitly registered with the ___ 18en stack. because
The spl_autoload_register() function replaces the succautoload function in Zend Engine with spl_autoload() or
spl_autoload_call ().
parameter
autoload_function
Automatic load function to be registered. If no arguments are provided, the default implementation function for autoload is automatically registered
spl_autoload ().
The return value
Returns TRUE on success and FALSE on failure.
Note: SPL is the abbreviation for Standard PHP Library(standard PHP library). It is an extension library introduced by PHP5. Its main functions include the implementation of the autoload mechanism and the inclusion of various Iterator interfaces or classes. The SPL autoload mechanism is implemented by pointing the function pointer autoload_func to its own implemented function with auto-load capability. SPL has two different functions, spl_autoload and spl_autoload_call, and achieves different auto-loading mechanisms by pointing autoload_func to these two different function addresses.
sample
Let's say we have a class file A.php that defines a class named A:

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

And then we have an index.ES74en and we need this class A, which is the normal way to write it

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

One problem, however, is that if our ES79en.php needs to contain not just class A, but many classes, it would have to write many lines of require, which is sometimes annoying.
However, after php5, we no longer need to do this. In php5, the autoload function is automatically called if you try to use an undefined class, so we can have php automatically load the class by writing the function with each successive 87en, rather than having to write a long list of containing files.
For example, in the above example, index. php could be written like this:

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

Of course, this is the simplest demonstration. ___ 95en will just go to include_path to find the class file and load it. We can define the rules of the load class with each ___ depending on our requirements.
Also, if we don't want to call ___ with auto-load and call our own function (or class method), we can register our own autoload function with spl_autoload_register. Its function prototype is as follows:
bool spl_autoload_register ( [callback $autoload_function] )
Let's rewrite the above example:

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

This should work fine, too, with php calling our own defined function loader instead of calling ___ when looking for a class. In the same way, you can write it this way:

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


Related articles: