这篇文章主要介绍了PHP中spl_autoload_register()函数用法,结合实例形式分析了__autoload函数及spl_autoload_register函数的相关使用技巧,需要的朋友可以参考下

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

一、__autoload

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

printit.class.php:

1

2

3

4

5

6

7

<?php

class PRINTIT {

 function doPrint() {

 echo 'hello world';

 }

}

?>

index.php

1

2

3

4

5

6

7

8

9

<?

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有与曲同工之妙,看个简单的例子:

1

2

3

4

5

6

7

8

9

10

<?

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() 可以调用静态方法

1

2

3

4

5

6

7

8

9

10

11

12

13

<?

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

PHP中 spl_autoload_register() 函数用法的更多相关文章

  1. PHP中spl_autoload_register()函数用法实例详解

    本文实例分析了PHP中spl_autoload_register()函数用法.分享给大家供大家参考,具体如下: 在了解这个函数之前先来看另一个函数:__autoload. 一.__autoload 这 ...

  2. Oracle 中 decode 函数用法

    Oracle 中 decode 函数用法 含义解释:decode(条件,值1,返回值1,值2,返回值2,...值n,返回值n,缺省值) 该函数的含义如下:IF 条件=值1 THEN RETURN(翻译 ...

  3. php中opendir函数用法实例

    这篇文章主要介绍了php中opendir函数用法,以实例形式详细讲述了opendir函数打开目录的用法及相关的注意事项,具有一定的参考借鉴价值,需要的朋友可以参考下 本文实例分析了php中opendi ...

  4. php中setcookie函数用法详解(转)

    php中setcookie函数用法详解:        php手册中对setcookie函数讲解的不是很清楚,下面是我做的一些整理,欢迎提出意见.        语法:        bool set ...

  5. 【313】python 中 print 函数用法总结

    参考:python 中 print 函数用法总结 参考:Python print() 函数(菜鸟教程) 参考:Python 3 print 函数用法总结 目录: 字符串和数值类型 变量 格式化输出 p ...

  6. PHP中is_*() 函数用法

    PHP中is_*() 函数用法 is_a - 如果对象属于该类或该类是此对象的父类则返回 TRUE is_array - 检测变量是否是数组 is_bool - 检测变量是否是布尔型 is_calla ...

  7. mysql中FIND_IN_SET函数用法

    本篇文章主要介绍mysql中FIND_IN_SET函数用法,用来精确查询字段中以逗号分隔的数据 以及其与 like 和 in 的区别 1.问题发现 之前在做工作任务时有这么一个需求:需要用接口所传的服 ...

  8. PHP中spl_autoload_register()函数的用法

    spl_autoload_register (PHP 5 >= 5.1.2) spl_autoload_register — 注册__autoload()函数 说明 bool spl_autol ...

  9. PHP中spl_autoload_register函数的用法

    spl_autoload_register (PHP 5 >= 5.1.2) spl_autoload_register — 注册__autoload()函数 说明bool spl_autolo ...

随机推荐

  1. 简单的layui二级联动

    用layui实现省市二级联动, 需要注意的是使用layui之后, 你看到的下拉选框就不是option了,而是一些div 1.select表单 2.JS, ajax返回的是普通的数组

  2. IntelliJ IDEA包层级结构显示方式

    在开发的过程中,程序结构增多,通过树状结构看包结构目录,更加舒适. Idea默认情况下是不分层级展示包结构的 点击设置标志按钮,如下图所示 去掉Hide Empty Middle Packages的勾 ...

  3. HDU 1690 最短路

    #include<stdio.h> #include<string.h> #include<queue> #include<algorithm> usi ...

  4. 【指南】本地如何搭建IPv6环境测试你的APP

    由于苹果最近更新IOS10之后他们的工作环境升级了,统一用IPV6网络,所以我们发出去的申请的APP不兼容IPV6的话,会通过不了审核! 所幸的是苹果会自动把你服务器要接的协议自动把iPV6转成IPV ...

  5. POJ1190 洛谷P1731 NOI1999 生日蛋糕

    生日蛋糕(蛋糕是谁?) Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 20272   Accepted: 7219 Desc ...

  6. C++:static类

    static自我理解 static使得数据成员或者函数生命周期为整个文件所在程序的生命周期, 在C中还可以用它避免被其它文件使用为外部成员 static类 明确:类的静态数据成员它被所有类对象共享,但 ...

  7. 盘点Apache毕业的11个顶级项目

    自1999年成立至今,Apache 软件基金会已成功建立起自己强大的生态圈.其社区涌现了非常多优秀的开源项目,同时有越来越多国内外项目走向这个国际开源社区进行孵化.据悉,目前所有的 Apache 项目 ...

  8. Ubuntu18.04+windows10双系统时间同步教程

    前言: 系统安装windows10和Ubuntu18.04双系统后会出现时间不同步的情况,往往windows系统的时间会有错误,一般会有8个小时的误差. 原因: 主要因为本地时间与硬件时间的时差: 本 ...

  9. php 获取客户端的ip、地理信息、浏览器信息、本地真实ip

    转自:http://www.blhere.com/948.html 这是非常实用的php常用类.获取客户端的ip.地理信息.浏览器信息.本地真实ip 1234567891011121314151617 ...

  10. Python学习之路7☞装饰器

    一:命名空间与作用域 1.1命名空间 局部命名空间: def foo(): x=1 def func(): pass 全局命名空间: import time class ClassName:pass ...