Example of spl_autoload_register () function usage in PHP, splregisterautoload

Source: Internet
Author: User
Tags autoload spl

Example of spl_autoload_register () function usage in PHP, splregisterautoload

This article analyzes the usage of the spl_autoload_register () function in PHP. We will share this with you for your reference. The details are as follows:

Before learning about this function, let's look at another function: __autoload.

1. _ autoload

This is an automatic loading function. In PHP5, when we instantiate an undefined class, this function is triggered. Take the following example:

Printit. class. php:

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

After index. php is run, hello world is output normally. In index. in php, printit is not included. class. php automatically calls the _ autoload function when instantiating printit. The value of $ class is the class name printit. class. php is introduced.

This method is often used in object-oriented systems to avoid writing too many referenced files and make the entire system more flexible.

Ii. spl_autoload_register ()

Let's look at spl_autoload_register (). This function is similar to _ autoload. Let's look at 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 _ autoload with the loadprint function. However, loadprint does not automatically trigger like _ autoload. In this case, spl_autoload_register () takes effect. It tells PHP to execute loadprint () when it encounters an unspecified class ().

Spl_autoload_register () call static methods

<? 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 method: spl_autoload_register ("test: loadprint"); $ obj = new PRINTIT (); $ obj-> doPrint ();?>

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 to the SPL _ autoload function stack. If the functions in the stack are not 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 Zend Engine with spl_autoload () or spl_autoload_call ().

Parameters

Autoload_function

The automatically loaded function to be registered. If no parameter is provided, the default implementation function spl_autoload () of autoload is automatically registered ().

Return Value

If the call succeeds, TRUE is returned. If the call fails, FALSE is returned.

Note: SPL is the abbreviation of 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 various Iterator interfaces or classes. The implementation of the SPL autoload mechanism is achieved by pointing the function pointer autoload_func to the self-implemented function with the automatic loading 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 function addresses.

ClassLOAD {staticfunctionloadClass ($ class_name) {$ filename = $ class_name. ". class. php "; $ path =" include /". $ filename if (is_file ($ path) returninclude $ path ;}}/*** sets the automatic loading of an object * spl_autoload_register-Register given function as _ autoload () implementation */spl_autoload_register (array ('load', 'loadclass');/*** _ autoload method will expire after spl_autoload_register, because the autoload_func function pointer has pointed to the spl_autoload Method *, you can use the following method to add the _ autoload method to the autoload_functions list */spl_autoload_register ('_ autoload ');

If a class method and the _ autoload function are registered with spl_autoload_register at the same time, the class file is loaded in the first registered method or function according to the registration sequence, it will no longer execute the method or function of the second registered class. Otherwise, the method or function of the second registered class will be executed.

<?phpclass autoloader {  public static $loader;  public static function init() {    if (self::$loader == NULL)      self::$loader = new self ();    return self::$loader;  }  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' ) );  }  public function library($class) {    set_include_path ( get_include_path () . PATH_SEPARATOR . '/lib/' );    spl_autoload_extensions ( '.library.php' );    spl_autoload ( $class );  }  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 );  }  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 );  }  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 );  }}//callautoloader::init ();?>

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.