Automatic load class usage of spl_autoload_register function in PHP

Source: Internet
Author: User
Tags autoload spl

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 in the SPL __autoload function stack. If the functions in the stack have not been activated, they are activated. If you have implemented the __autoload function in your program, it must be explicitly registered in the __autoload stack. Because the Spl_autoload_register () function replaces the __autoload function in Zend engine with spl_autoload () or Spl_autoload_call (). ParametersAutoload_function
The auto-load function to register. If no arguments are provided, the default implementation function Spl_autoload () for AutoLoad is automatically registered. return value
Returns TRUE if successful, and FALSE if it fails. Note: SPL is the abbreviation for standard PHP library (PHP libraries). It is an extension library introduced by PHP5, whose main functions include the implementation of the AutoLoad mechanism and the inclusion of various iterator interfaces or classes. The implementation of the SPL autoload mechanism is achieved by pointing the function pointer autoload_func to its own implementation of the function that has the automatic loading function. SPL has two different function spl_autoload, Spl_autoload_call, to implement different automatic loading mechanisms by pointing autoload_func to these two different function addresses. Example

We have a class file a.php, which defines a class named a:

View Plaincopy to Clipboardprint?
<?php
Class A
{
Public Function __construct ()
{
Echo ' Got it. ';
}
}


Then we have a index.php need to use this class A, the general way of writing is

View Plaincopy to Clipboardprint?
<?php
Require (' a.php ');
$a = new A ();


But one problem is that if our index.php need to include more than just class A, we need a lot of classes, and this way we have to write a lot of require statements, which sometimes makes people feel uncomfortable.


But in the post-php5 version, we don't need to do that anymore. In PhP5, when trying to use a class that has not yet been defined, the __autoload function is called automatically, so we can write the __autoload function to let PHP automatically load the class without having to write a long list of included files.

For example, in the example above, index.php can write:

View Plaincopy to Clipboardprint?
<?php
function __autoload ($class)
{
$file = $class. '. php ';
if (Is_file ($file)) {
Require_once ($file);
}
}

$a = new A ();

Of course, above is just the simplest demonstration, __autoload just go to Include_path to find the class file and load, we can define the rules of __autoload load class according to our own needs.

In addition, if we do not want to invoke __autoload when loading automatically, but instead 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])

We continue to rewrite the example above:

View Plaincopy to Clipboardprint?
<?php
function Loader ($class)
{
$file = $class. '. php ';
if (Is_file ($file)) {
Require_once ($file);
}
}

Spl_autoload_register (' loader ');

$a = new A ();

This can also work properly, when PHP is looking for classes without calling __autoload instead of calling our own defined function loader. In the same way, the following is also possible:

View Plaincopy to Clipboardprint?
<?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 ();

Automatic load class usage of spl_autoload_register function in PHP

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.