Introduction to the PHP function spl_autoload_register of and s 5en of

  • 2020-05-12 02:21:58
  • OfStack

The usage of s 1en () is not mentioned again, which has already been mentioned in my WEB development notes. PHP __autoload function (automatic load the class file) method of use, the original address: / / www ofstack. com article / 29625. htm.

S 12en_autoload_register (), easy to understand, is to declare a custom s 16en function, s 16en function, s 17en function, s 19en function, s 18en () function.

This method is called when PHP cannot find the class file. Instead of calling the function s 24en (), PHP will call the custom function s when its own function s or methods are registered

spl_autoload_register (' func_name ');
spl_autoload_register (array (' class_name ', 'method_name'));

Details are as follows:

spl_autoload_register
(PHP 5 > = 5.1.2)
spl_autoload_register -- register with the function s 54en ()
instructions
bool spl_autoload_register ([ callback $autoload_function ] )
Register the function in the SPL s 60en function stack. If the functions in the stack are not already active, activate them.
If you have already implemented the s 62en function in your program, it must be explicitly registered in the s 63en stack. because
The spl_autoload_register() function will replace the Zend Engine function with spl_autoload() or
spl_autoload_call ().
parameter
autoload_function
Auto load function to register. If no parameters are provided, the default implementation function of autoload is automatically registered
spl_autoload ().
The return value
Return TRUE on success and FALSE on failure.
Note: SPL is short for Standard PHP Library(standard PHP library). It is an extended library introduced by PHP5. Its main functions include the implementation of autoload mechanism and various Iterator interfaces or classes. The SPL autoload mechanism is implemented by pointing the function pointer autoload_func to a self-implemented function with automatic loading. SPL has two different functions, spl_autoload and spl_autoload_call, and different auto-loading mechanisms are implemented by pointing autoload_func to these two different function addresses.

The code is as follows:

test.class.php


<?php 
class abc{ 
function __construct() 
{ 
echo 'www.chhua.com; 
} 
} 
?>

load.php


<?php 
class LOAD 
{ 
static function loadClass($class_name) 
{ 
$filename = $class_name.".class.php"; 
if (is_file($filename)) return include_once $filename; 
} 
} 
/** 
*  Sets the automatic loading of the object  
* spl_autoload_register  -  Register given function as __autoload() implementation 
*/ 
spl_autoload_register(array('LOAD', 'loadClass')); 
$a = new Test();// Implement automatic loading, which is the way many frameworks use to automatically load classes  
?> 

The correct way to write the spl_autoload_register loading function

AutoLoading\loading


<?php
namespace AutoLoading;

class Loadind {
 public static function autoload($className){
        // According to the PSR-O The first 4 point   the  \  Conversion layer (directory style character)    DIRECTORY_SEPARATOR , 
    // Easy to compatible Linux File to find. Windows  ( /  and  \ ) is universal 
    // Due to the namspace  It's a big specification, so you can find it straight away 
    $fileName = str_replace('\\', DIRECTORY_SEPARATOR, DIR . '\\'. $className) . '.php';
    if (is_file($fileName)) {
      require $fileName;
    } else {
      echo $fileName . ' is not exist'; die;
    }    }   
}

index.php


// Defines the absolute path to the current directory 
define('DIR', dirname(__FILE__));
// Load this file 
require DIR . '/loading.php';
// using ` The namespace ` The way to register. php 5.3  To join the 
// It has to be static Static method calls, and then it's like loading namespace Way to call, note: cannot be used use
spl_autoload_register("\\AutoLoading\\loading::autoload"); 
//  call 3 a namespace class 
// Locate the Lib In the directory Name.php 
Lib\Name::test();
// Locate the App directory Android In the directory Name.php
App\Android\Name::test();
// Locate the App directory Ios In the directory Name.php
App\Ios\Name::test();


Related articles: