Home>

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

First, __autoload

This is an autoload 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 ();?>

Running index.php normally outputs hello world. In index.php, since printit.class.php is not included, when printit is instantiated, the __autoload function is automatically called, and the value of the parameter $class is the class name printit. At this time, printit.class.php was introduced. .

This method is often used in object-oriented,Can avoid writing too many references,It also makes the whole system more flexible.

Second, spl_autoload_register ()

Look at spl_autoload_register () again. This function works the same as __autoload.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. But loadprint will not trigger automatically like __autoload,At this point spl_autoload_register () works.It tells php to execute loadprint () when it encounters an undefined class.

spl_autoload_register () calls a 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 — register __autoload () function

Description

bool spl_autoload_register ([callback $autoload_function])

Register the function in the spl __autoload function stack.If the functions in this stack have not been activated,Activate them.

If the __autoload function has been implemented in your program, it must be explicitly registered in the __autoload stack. Because spl_autoload_register () function will replace __autoload function in zend engine with spl_autoload () or spl_autoload_call ()

parameter

autoload_function

The autoload function to register.If no parameters are provided,Automatically register the default implementation function spl_autoload () of autoload.

return value

Returns true if successful, false otherwise.

Note:spl is an abbreviation for standard php library. It is an extension library introduced by php5,Its main functions include the implementation of the autoload mechanism and include various iterator interfaces or classes.The implementation of the spl autoload mechanism is achieved by pointing a function pointer autoload_func to a function with an autoload function implemented by itself.spl has two different functions spl_autoload, spl_autoload_call, and implements different autoloading mechanisms by pointing 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 auto loading of objects
 * spl_autoload_register — register given function as __autoload () implementation
 * /
spl_autoload_register (array ("load", "loadclass"));
/**
* __ autoload method will fail after spl_autoload_register,Because the autoload_func function pointer already points to the spl_autoload method
* You can add the _autoload method to the autoload_functions list by the following method
* /
spl_autoload_register ("__autoload");

If a class method and __autoload function are registered with spl_autoload_register at the same time, according to the order of registration,If a class file is loaded in the first registered method or function,No more methods or functions of the second registered class will be executed.Otherwise, the method or function of the second registered class will be executed.

<?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 ();
?>
php
  • Previous Another wonderful barrage effect jQuery implementation
  • Next Some comprehensive understanding of Js OOP programming to create objects