The difference between Spl_autoload_register and autoload _php skills

Source: Internet
Author: User
Tags autoload function prototype spl
Spl_autoload_register
(PHP 5 >= 5.1.2)
spl_autoload_register-Registration __autoload () function
description
BOOL Spl_autoload_register ([callback $autoload _function])
Registers a function into the SPL __autoload function stack. If the functions in the stack have not been activated, activate them.
If the __autoload function has been implemented in your program, it must be explicitly registered to the __autoload stack. Because
The Spl_autoload_register () function replaces the __autoload function in the Zend engine with the spl_autoload () or
Spl_autoload_call ().
parameters
Autoload_function
Automatic load function to register. Automatically registers the default implementation function for AutoLoad if no arguments are supplied
Spl_autoload ().
return value
Returns TRUE if successful, and returns FALSE if it fails.
Note: SPL is an abbreviation for the standard PHP library (standard PHP libraries). It is an extension library introduced by PHP5, and its main functions include the implementation of autoload mechanism and including various iterator interfaces or classes. The implementation of the SPL autoload mechanism is achieved by pointing the function pointer Autoload_func to the function that has automatic loading function. SPL has two different functions spl_autoload, Spl_autoload_call, to implement different automatic loading mechanisms by pointing the autoload_func to these two different function addresses.
Example
Let's have a class file a.php, which defines a class named a:
Copy Code code as follows:

<?php
Class A
{
Public Function __construct ()
{
Echo ' Got it. '
}
}

And then we have a index.php need to use this class A, and the usual way to do that is
Copy Code code as follows:

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

But one problem is that if our index.php needs to contain more than just class A, it needs a lot of classes, and it's going to have to write a lot of lines and require statements, and sometimes it can be frustrating.
But after the PHP5 version, we don't need to do that anymore. In php5, the AutoLoad function is automatically invoked when trying to use a class that is not yet defined, so we can write the __autoload function to let PHP load the class automatically without having to write a long list of included files.
For example, in the above example, index.php can write this:
Copy Code code as follows:

<?php
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 go include_path looking for class files and loading, we can define the __autoload load class rules According to our own needs.
Also, if we don't want to invoke __autoload when we don't load it automatically, we call our own functions (or class methods) and we can use Spl_autoload_register to register our own autoload functions. Its function prototype is as follows:
BOOL Spl_autoload_register ([callback $autoload _function])
We continue to rewrite the above example:
Copy Code code as follows:

<?php
function Loader ($class)
{
$file = $class. '. php ';
if (Is_file ($file)) {
Require_once ($file);
}
}
Spl_autoload_register (' loader ');
$a = new A ();

Such a child is also able to run normally, at this time PHP is looking for a class when it does not invoke __autoload but call our own defined function loader. In the same way, the following wording is also possible:
Copy Code code as follows:

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.