php自动加载

PHP实现自动加载,有两种方法:

  1、魔术函数 __autoload()

  2、spl扩展 spl_autoload_register

分别举例说明:

一、__autoload

  printit.class.php:

1 <?php
2 class PRINTIT {
3  function doPrint() {
4  echo 'hello world';
5  }
6 }

  index.php:

1 <?php
2 function __autoload( $class ) {
3      $file = $class . '.class.php';
4      if ( is_file($file) ) {
5      require_once($file);
6      }
7 }
8 $obj = new PRINTIT();
9 $obj->doPrint();

当我们每次运行index.php后都会正常输出hello world,尽管我们并没有在index.php中引入printit.class.php文件,但是我们依然能够用PRINTIT类内的方法。

在index.php中,由于没有包含printit.class.php,在实例化printit时,自动调用__autoload函数,参数$class的值即为类名printit,此时printit.class.php就被引进来了。

在面向对象中这种方法经常使用,可以避免书写过多的引用文件,同时也使整个系统更加灵活。

二、spl_autoload_register()

 1 <?php
 2 function loadprint( $class ) {
 3    $file = $class . '.class.php';
 4     if (is_file($file)) {
 5       require_once($file);
 6     }
 7 }
 8 spl_autoload_register( 'loadprint' );
 9 $obj = new PRINTIT();
10 $obj->doPrint();

将__autoload换成loadprint函数。但是loadprint不会像__autoload自动触发,这时spl_autoload_register()就起作用了,它告诉PHP碰到没有定义的类就执行loadprint()。
spl_autoload_register() 调用静态方法.

 1 <?php
 2 class test {
 3      public static function loadprint( $class ) {
 4      $file = $class . '.class.php';
 5          if (is_file($file)) {
 6               require_once($file);
 7          }
 8      }
 9 }
10 spl_autoload_register( array('test','loadprint') );
11 //另一种写法:spl_autoload_register( "test::loadprint" );
12 $obj = new PRINTIT();
13 $obj->doPrint();

注:SPL是Standard PHP Library(标准PHP库)的缩写。它是PHP5引入的一个扩展库,其主要功能包括autoload机制的实现及包括各种Iterator接口或类。SPL autoload机制的实现是通过将函数指针autoload_func指向自己实现的具有自动装载功能的函数来实现的。SPL有两个不同的函数spl_autoload, spl_autoload_call,通过将autoload_func指向这两个不同的函数地址来实现不同的自动加载机制。

 1 classLoad
 2 {
 3      function static  loadClass($class_name){
 4             $filename= $class_name.".class.php";
 5             $path= "include/".$filename
 6             if(is_file($path)) returninclude$path;
 7       }
 8 }
 9 /**
10  * 设置对象的自动载入
11  * spl_autoload_register — Register given function as __autoload() implementation
12  */
13 spl_autoload_register(array('LOAD', 'loadClass'));
14 /**
15 *__autoload 方法在 spl_autoload_register 后会失效,因为 autoload_func 函数指针已指向 spl_autoload 方法
16 * 可以通过下面的方法来把 _autoload 方法加入 autoload_functions list
17 */
18 spl_autoload_register( '__autoload');                         

如果同时用spl_autoload_register注册了一个类的方法和__autoload函数,那么,会根据注册的先后,如果在第一个注册的方法或函数里加载了类文件,就不会再执行第二个被注册的类的方法或函数。反之就会执行第二个被注册的类的方法或函数。

 1 <?php
 2 class autoloader {
 3   public static $loader;
 4   public static function init() {
 5     if (self::$loader == NULL)
 6       self::$loader = new self ();
 7     return self::$loader;
 8   }
 9   public function __construct() {
10     spl_autoload_register ( array ($this, 'model' ) );
11     spl_autoload_register ( array ($this, 'helper' ) );
12     spl_autoload_register ( array ($this, 'controller' ) );
13     spl_autoload_register ( array ($this, 'library' ) );
14   }
15   public function library($class) {
16     set_include_path ( get_include_path () . PATH_SEPARATOR . '/lib/' );
17     spl_autoload_extensions ( '.library.php' );
18     spl_autoload ( $class );
19   }
20   public function controller($class) {
21     $class = preg_replace ( '/_controller$/ui', '', $class );
22     set_include_path ( get_include_path () . PATH_SEPARATOR . '/controller/' );
23     spl_autoload_extensions ( '.controller.php' );
24     spl_autoload ( $class );
25   }
26   public function model($class) {
27     $class = preg_replace ( '/_model$/ui', '', $class );
28     set_include_path ( get_include_path () . PATH_SEPARATOR . '/model/' );
29     spl_autoload_extensions ( '.model.php' );
30     spl_autoload ( $class );
31   }
32   public function helper($class) {
33     $class = preg_replace ( '/_helper$/ui', '', $class );
34     set_include_path ( get_include_path () . PATH_SEPARATOR . '/helper/' );
35     spl_autoload_extensions ( '.helper.php' );
36     spl_autoload ( $class );
37   }
38 }
39 //call
40 autoloader::init ();

官方文档:https://www.php.net/manual/zh/function.spl-autoload-register.php

原文地址:https://www.cnblogs.com/katu/p/11454269.html