PHP spl_autoload_register () function usage examples to explain _php skills

Source: Internet
Author: User

This article analyzes the usage of spl_autoload_register () function in PHP. Share to everyone for your reference, specific as follows:

Let's look at another function before you know this function: __autoload.

First, __autoload

This is an auto load function, in PHP5, when we instantiate an undefined class, this function is triggered. Look at the following example:

printit.class.php:

<?php
class PrintIt {
 function doprint () {
 echo ' Hello World ';
 }
}
? >

index.php

?
function __autoload ($class) {
 $file = $class. '. class.php ';
 if (Is_file ($file)) {
 require_once ($file);
 }
}
$obj = new PrintIt ();
$obj->doprint ();? >

Normal output Hello world after running index.php. In index.php, the __autoload function is automatically invoked when the PrintIt is instantiated, and the value of the parameter $class is the class name PrintIt, because the printit.class.php is not included. At this time printit.class.php was introduced.

This approach is often used in object-oriented, avoiding the writing of too many reference files and making the system more flexible.

Second, Spl_autoload_register ()

Then look at Spl_autoload_register (), this function and __autoload have the wonderful Kutong, see a simple example:

?
function Loadprint ($class) {
 $file = $class. '. class.php ';
 if (Is_file ($file)) {
 require_once ($file);
 }
}
Spl_autoload_register (' Loadprint ');
$obj = new PrintIt ();
$obj->doprint ();? >

Replace the __autoload with the Loadprint function. But Loadprint does not automatically trigger like __autoload, when Spl_autoload_register () works, which tells PHP to run Loadprint () when it encounters a class that is not defined.

Spl_autoload_register () calls the static method

?
Class Test {public
 static function Loadprint ($class) {
 $file = $class. '. class.php ';
 if (Is_file ($file)) {
  require_once ($file);
}}} Spl_autoload_register (Array (' Test ', ' loadprint '));
Another way of writing: Spl_autoload_register ("Test::loadprint");
$obj = new PrintIt ();
$obj->doprint ();? >

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 spl_autoload () or Spl_autoload_call ().

Parameters

Autoload_function

Automatic load function to register. If no arguments are supplied, the default implementation function Spl_autoload () for the autoload is automatically registered.

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.

Classload
{
 staticfunctionloadclass ($class _name)
  {
    $filename = $class _name. ". Class.php ";
 $path = "include/". $filename
    if (Is_file ($path)) Returninclude$path
  }
}
/**
 * Set Object automatic loading
 * Spl_autoload_register-register given function as __autoload () implementation * *
spl_autoload_register (Array (' LOAD ', ' loadclass '));
The/**
*__autoload method will fail after Spl_autoload_register, because the AUTOLOAD_FUNC function pointer has pointed to the Spl_autoload method
* Can be used in the following way AutoLoad method Add autoload_functions List
* * *
spl_autoload_register (' __autoload ');

If the method of a class and the __autoload function are registered with Spl_autoload_register, then the method or function of the second registered class will not be executed if the class file is loaded in the first registered method or function, depending on the registration. Conversely, the method or function of the second registered class is executed.

<?php class Autoloader {public static $loader;
    public static function init () {if (self:: $loader = = NULL) Self:: $loader = new self ();
  Return self:: $loader;
    The Public Function __construct () {spl_autoload_register (Array ($this, ' model '));
    Spl_autoload_register (Array ($this, ' helper '));
    Spl_autoload_register (Array ($this, ' Controller '));
  Spl_autoload_register (Array ($this, ' library ')); The Public Function library ($class) {Set_include_path () Get_include_path (). Path_separator.
    '/lib/');
    Spl_autoload_extensions ('. library.php ');
  Spl_autoload ($class);
    The Public Function controller ($class) {$class = preg_replace ('/_controller$/ui ', ', ', $class); Set_include_path (Get_include_path (). Path_separator.
    '/controller/');
    Spl_autoload_extensions ('. controller.php ');
  Spl_autoload ($class);
    The Public Function model ($class) {$class = preg_replace ('/_model$/ui ', ', ', $class);Set_include_path (Get_include_path (). Path_separator.
    '/model/');
    Spl_autoload_extensions ('. model.php ');
  Spl_autoload ($class);
    The Public Function Helper ($class) {$class = preg_replace ('/_helper$/ui ', ', ', $class); Set_include_path (Get_include_path (). Path_separator.
    '/helper/');
    Spl_autoload_extensions ('. helper.php ');
  Spl_autoload ($class);

 }//call Autoloader::init ();?>

For more information about PHP interested readers can view the site topics: "PHP object-oriented Programming Introductory Course", "PHP Mathematical Arithmetic Skills summary", "PHP Array" Operation Techniques Encyclopedia, "PHP string (String) Usage Summary", " PHP Data structure and algorithm tutorial, "PHP Programming Algorithm Summary", "PHP Regular Expression Usage Summary", and "PHP Common database Operation Skills Summary"

I hope this article will help you with the PHP program design.

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.