在了解这个函数之前先来看另一个函数:__autoload。

一、__autoload

这是一个自动加载函数,在PHP5中,当我们实例化一个未定义的类时,就会触发此函数。看下面例子:

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

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

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

二、spl_autoload_register()

再看spl_autoload_register(),这个函数与__autoload有与曲同工之妙,看个简单的例子:

  
 
<?
function loadprint( $class ) {
 $file $class '.class.php';  
 if (is_file($file)) {  
  require_once($file);  
 
 
spl_autoload_register( 'loadprint' ); 
 
$obj new PRINTIT();
$obj->doPrint();
?>

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

spl_autoload_register() 调用静态方法

  
 
<? 
 
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 spl_autoload_register的用法的例子有很多很多,自己也查看了很多,但感觉介绍得并不太详细,使自己真正能明白其中的原理苦闷了好一会儿。现将自己的理解记录下来。

关于 Standard PHP Library (SPL) 的 autoload 的方法,这些都是 PHP 5.1.2 之后才加上的方法。为了方便,这里做了一些设定。假设你有类文件,放在/home/user/class/foo.class.php, 你当前的文件为/home/user/webroot/test.php, 示例代码如下。

在文件test.php中:

 1 <?php
 2
 3 class autoload
 4 {
 5   public static function load( $class name )
 6   {
 7     $filename = "/home/user/class/".$classname."class.php";
 8     if (file_exists($filename )) {
 9       require_once $filename ;
10     }
11   }
12 }
13
14 function __autoload( $class name )
15 { // 这个是默认的 autoload 方法
16     $filename = "/home/user/class/".$classname."class.php";
17     if (file_exists($filename )) {
18       require_once $filename ;
19   }
20 }
21
22 // 注册一个 autoloader
23 spl_autoload_register( 'autoload::load' );
24 /**
25 * __autoload 方法在 spl_autoload_register 后会失效,因为 autoload_func 函数指针已指向 spl_autoload 方法
26 * 可以通过下面的方法来把 _autoload 方法加入 autoload_functions list
27 */
28 spl_autoload_register( '__autoload' );
29  // 注:下面的类看上去没有定义,但其实系统根据sql_autoload_register提供的路径会自动去/home/user// /class/*.class.php下搜索foo.class.php文件,如果没找到才报错。
30 $foo = new foo();
31 $foo ->bar();
32 ?>

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

spl_autoload_register 是注册php auto_load的函数,这个函数可以多次加载

每一个函数应该都有返回值(boolean),如果返回值为true则认为已经加载成功就退出了加载过程,如果失败则继续调用后边的auto_load函数加载php文件,当然如果最后一个auto_load也没有加载成功这时候就没有加载完成,new的时候就会报错。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
<?php
/**
 * Created by PhpStorm.
 * User: chengtao
 * Date: 14-7-3
 * Time: 上午11:27
 */
define('BASE_PATH',dirname(__FILE__).'/') ;
function cron_autoload1 ($name) {
    $file = BASE_PATH.'lib1/'.$name.'.class.php';
    if(file_exists($file)){
        include_once($file);
        return true;
    }
}
function cron_autoload2 ($name) {
    $file = BASE_PATH.'lib2/'.$name.'.class.php';
    if(file_exists($file)){
        include_once($file);
        return true;
    }
}
spl_autoload_register('cron_autoload1');
spl_autoload_register('cron_autoload2');
new Class1();
new Class2();

详解spl_autoload_register()函数相关推荐

  1. php 详解spl_autoload_register()函数

    在了解这个函数之前先来看另一个函数:__autoload. 一.__autoload 这是一个自动加载函数,在PHP5中,当我们实例化一个未定义的类时,就会触发此函数.看下面例子: printit.c ...

  2. 一分钟详解initUndistortRectifyMap函数bug修复方法

    本文首发于微信公众号「3D视觉工坊」--一分钟详解initUndistortRectifyMap函数bug修复方法 在上一篇文章OpenCV中initUndistortRectifyMap函数存在bu ...

  3. python命名空间和闭包_Python函数基础实例详解【函数嵌套,命名空间,函数对象,闭包函数等】...

    本文实例讲述了Python函数基础用法.分享给大家供大家参考,具体如下: 一.什么是命名关键字参数? 格式: 在*后面参数都是命名关键字参数. 特点: 1.约束函数的调用者必须按照Kye=value的 ...

  4. 详解虚函数的实现过程之菱形继承(5)

    大家看到标题,会不会菱形继承的虚表会不会是重复的呢?祖父类的虚表会不会在子类会不会是两份相同呢?那么我们一起来探索一下吧,冲冲冲!! 首先我们来分析一下: 它一共定义了四个类,分别为CFurnitur ...

  5. 详解虚函数的实现过程之虚基类(4)

    博客虚函数实现过程3 时提到过虚基类,这里呢,我们来详细讲述一下: 当我们在虚函数的声明结尾处添加"=0",这种虚函数就被称为纯虚函数. 它好似一个没有实现只有声明的函数,它的存在 ...

  6. 详解虚函数的实现过程之多重继承(3)

    下面来一起探索一下多重继承时,有虚函数会怎么继承呢? 这里大家猜一下,SofaBed会占多少个字节呢? 首先我们是不是得猜一下它有几个虚表指针? 4* 4(4个int数据)+2*4(两个虚表指针)=2 ...

  7. 详解虚函数的实现过程之单继承(2)

    从汇编分析一下下面的多态模拟结构 利用 父类指针指向子类的特性,可以间接调用各子类中的虚函数. 虽然指针类型为父类,但由于虚表的排列顺序是按虚函数在类继承层次中首次声明的顺序依次排列的,因此,只要继承 ...

  8. 详解虚函数的实现过程之初探虚表(1)

    空对象它有一字节的大小,在没有任何成员变量但是却有虚函数的对象里,它的大小是四个字节,这是为什么呢? 因为含有虚函数的对象里,对象的起始地址往后四个字节其实是 一个指针,它指向了一个数组,这个数组的元 ...

  9. 详解Scala函数也是对象的特性

    详解Scala函数也是对象的特性

最新文章

  1. 在建工程直接费用化_计入在建工程的成本怎么算
  2. C语言——常见的字符串函数+内存操作函数的介绍及实现
  3. 一款神仙接私活儿软件,吊到不行!
  4. Visual studio 打包
  5. java sin函数图像_java中怎样绘制正弦函数图象
  6. 自动适配autolayout
  7. 【转】:localStorage使用总结
  8. redis常用命令与常用api
  9. 国内智能手机市场寒风凛冽,华米OV谁更受伤?
  10. 押上声誉百亿豪赌,雷军几分胜算?
  11. Oracle误删除表空间的恢复
  12. UI实用素材|衬线字体素材的应用要点
  13. 微观机器人会使用激光脉冲穿过人体
  14. 实验室NEWIFI-D1路由小云系统简易配置参考
  15. 易灵思FPGA--Programming Mode
  16. python——飞机大战小游戏
  17. DAY2-python数据类型、字符编码、文件处理
  18. Vue3+TS项目中element-plus自动导入组件后,找不到文件
  19. 臻和科技与依莫纳医疗共同开发mRNA肿瘤疫苗;赛多利斯中国应用与服务中心落地上海张江 | 医药健闻...
  20. mysql查询 多门课程的平均成绩_Mysql_多表查询练习

热门文章

  1. Paxos一致性协议
  2. AIX 7.1 使用installp安装python的方法
  3. resin常见有关问题
  4. 蓝色版苹果iPhone 12开箱上手视频流出;谷歌回应司法部反垄断诉讼:存在严重漏洞;​Git 2.29 稳定版发布|极客头条
  5. ldap集成nginx
  6. HTML5 Drop API
  7. 12月23号 Foundation库NSString操作
  8. vim7.4官方源码在vs2013的编译方法及问题总结
  9. java: 十六进制转八进制
  10. 嵌入式Linux文件提取,嵌入式 Linux系统编程(四)——文件属性