Spl_autoload_register in PHP () function usage example explanation

Before looking at this function first look at another function: __ autoload.
A, __ autoload
This is an automatic loading function, in PHP5, when we instantiate an undefined class, will trigger this function. 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 run index.php. In index.php, since no printit.class.php comprising, when instantiating printit, __autoload automatically call the function, the value of the parameter is the class name $ class printit, this time it was introduced to the printit.class.php .
This method is often used in object-oriented, to avoid writing too many references, but also make the whole system more flexible.
Two, spl_autoload_register ()
look spl_autoload_register (), this function is __autoload have wonderful co-workers with the song, 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();?> 

The __autoload replaced loadprint function. But loadprint not automatically trigger like __autoload, then spl_autoload_register () to play a role, it tells PHP class does not define a hit on the implementation of loadprint ().
spl_autoload_register () static method call

<?
class test { public static function loadprint( $class ) { $file = $class . '.class.php'; if (is_file($file)) { require_once($file); } } } spl_autoload_register( array('test','loadprint') ); //另一种写法:spl_autoload_register( "test::loadprint" ); $obj = new PRINTIT(); $obj->doPrint();?> 

spl_autoload_register
(the PHP. 5> = 5.1.2)
spl_autoload_register - Sign the __autoload () function
described
bool spl_autoload_register ([callback $ autoload_function] )
will be a function of the SPL __autoload function register stack. If the function of the stack has not been activated, activate them.
If your program has achieved __autoload function, it must be explicitly registered to __autoload stack. Since spl_autoload_register () function will Zend Engine is substituted in the spl_autoload __autoload function () or spl_autoload_call ().
Parameter
autoload_function
To register the automatic loading function. If you do not provide any parameters, then automatically registered default implementation autoload function spl_autoload ().
Return value
if successful returns TRUE, FALSE on failure.
Note: SPL is the abbreviation for Standard PHP Library (Standard PHP Library) is. It is an extension library PHP5 introduced to achieve its main function includes autoload Iterator mechanism and includes various classes or interfaces. Achieve SPL autoload mechanism is implemented by the function pointer to a function autoload_func automatic loading function with their implementation. SPL has two different functions spl_autoload, spl_autoload_call, to achieve different automatic loading mechanism by autoload_func point to these two different functions address.

classLOAD
{
 staticfunctionloadClass($class_name)
  {
    $filename= $class_name.".class.php";
 $path= "include/".$filename
    if(is_file($path)) returninclude$path;
  }
}
/**
 * 设置对象的自动载入
 * spl_autoload_register — Register given function as __autoload() implementation
 */
spl_autoload_register(array('LOAD', 'loadClass')); /** *__autoload 方法在 spl_autoload_register 后会失效,因为 autoload_func 函数指针已指向 spl_autoload 方法 * 可以通过下面的方法来把 _autoload 方法加入 autoload_functions list */ spl_autoload_register( '__autoload'); 

If both methods are registered with spl_autoload_register and __autoload a class function, then, will be registered under the order, if the class file is loaded in the first registration method or function, the execution will not be registered in the second class method or function. Conversely executes a second class of methods to be registered or function.

<?php
class 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 ); } } //call autoloader::init (); ?>

Guess you like

Origin www.cnblogs.com/linqingvoe/p/10937709.html