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


Posted in PHP onJuly 18, 2016

本文实例分析了PHP中spl_autoload_register()函数用法。分享给大家供大家参考,具体如下:

在了解这个函数之前先来看另一个函数:__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_autoload_register

(PHP 5 >= 5.1.2)

spl_autoload_register — 注册__autoload()函数

说明

bool spl_autoload_register ([ callback $autoload_function ] )
将函数注册到SPL __autoload函数栈中。如果该栈中的函数尚未激活,则激活它们。

如果在你的程序中已经实现了__autoload函数,它必须显式注册到__autoload栈中。因为spl_autoload_register()函数会将Zend Engine中的__autoload函数取代为spl_autoload() 或 spl_autoload_call()。

参数

autoload_function

欲注册的自动装载函数。如果没有提供任何参数,则自动注册autoload的默认实现函数spl_autoload()。

返回值

如果成功则返回 TRUE,失败则返回 FALSE。

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

classLOAD
{
 staticfunctionloadClass($class_name)
  {
    $filename= $class_name.".class.php";
 $path= "include/".$filename
    if(is_file($path)) returninclude$path;
  }
}
/**
 * 设置对象的自动载入
 * spl_autoload_register — Register given function as __autoload() implementation
 */
spl_autoload_register(array('LOAD', 'loadClass'));
/**
*__autoload 方法在 spl_autoload_register 后会失效,因为 autoload_func 函数指针已指向 spl_autoload 方法
* 可以通过下面的方法来把 _autoload 方法加入 autoload_functions list
*/
spl_autoload_register( '__autoload');

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

<?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程序设计有所帮助。

PHP 相关文章推荐
php.ini中date.timezone设置分析
Jul 29 PHP
php实现cc攻击防御和防止快速刷新页面示例
Feb 13 PHP
初识PHP
Sep 28 PHP
2014最热门的24个php类库汇总
Dec 18 PHP
PHP浮点数精度问题汇总
May 13 PHP
fsockopen pfsockopen函数被禁用,SMTP发送邮件不正常的解决方法
Sep 20 PHP
JSON字符串传到后台PHP处理问题的解决方法
Jun 05 PHP
PHP中的函数声明与使用详解
May 27 PHP
php实现的双色球算法示例
Jun 20 PHP
PHP使用微信开发模式实现搜索已发送图文及匹配关键字回复的方法
Sep 13 PHP
用Laravel Sms实现laravel短信验证码的发送的实现
Nov 29 PHP
PHP设计模式(八)装饰器模式Decorator实例详解【结构型】
May 02 PHP
详谈PHP程序Laravel 5框架的优化技巧
Jul 18 #PHP
3种方法轻松处理php开发中emoji表情的问题
Jul 18 #PHP
PHP生成图像验证码的方法小结(2种方法)
Jul 18 #PHP
Yii2中DropDownList简单用法示例
Jul 18 #PHP
Yii2使用dropdownlist实现地区三级联动功能的方法
Jul 18 #PHP
Yii2框架dropDownList下拉菜单用法实例分析
Jul 18 #PHP
用HTML/JS/PHP方式实现页面延时跳转的简单实例
Jul 18 #PHP
You might like
PHP文件缓存类实现代码
2015/10/26 PHP
php判断当前操作系统类型
2015/10/28 PHP
解析PHP的Yii框架中cookie和session功能的相关操作
2016/03/17 PHP
PHP实现在数据库百万条数据中随机获取20条记录的方法
2017/04/19 PHP
详解php中的implements 使用
2017/06/13 PHP
PHP封装的完整分页类示例
2018/08/21 PHP
CI框架实现创建自定义类库的方法
2018/12/25 PHP
经常用到的JavasScript事件的翻译
2007/04/09 Javascript
对采用动态原型方式无法展示继承机制得思考
2009/12/04 Javascript
Javascript 学习笔记之 对象篇(二) : 原型对象
2014/06/24 Javascript
extjs 时间范围选择自动判断的实现代码
2014/06/24 Javascript
浅析JS运动
2015/12/28 Javascript
express文件上传中间件Multer详解
2016/10/24 Javascript
如何学JavaScript?前辈的经验之谈
2016/12/28 Javascript
详解node如何让一个端口同时支持https与http
2017/07/04 Javascript
jQuery+Ajax请求本地数据加载商品列表页并跳转详情页的实现方法
2017/07/12 jQuery
vue input输入框模糊查询的示例代码
2018/05/22 Javascript
详解Webstorm 下的Angular2.0开发之路(图文)
2018/12/06 Javascript
原生js代码能实现call和bind吗
2019/07/31 Javascript
Python的Django框架中URLconf相关的一些技巧整理
2015/07/18 Python
python开发环境PyScripter中文乱码问题解决方案
2016/09/11 Python
flask中主动抛出异常及统一异常处理代码示例
2018/01/18 Python
Python3日期与时间戳转换的几种方法详解
2019/06/04 Python
Python Selenium 之数据驱动测试的实现
2019/08/01 Python
python求质数列表的例子
2019/11/24 Python
聊聊python中的异常嵌套
2020/09/01 Python
兰蔻法国官方网站:Lancôme法国
2020/02/22 全球购物
医学生自我鉴定范文
2013/11/08 职场文书
优秀交警事迹材料
2014/01/26 职场文书
资源工程专业毕业生求职信
2014/02/27 职场文书
销售经理岗位职责
2014/03/16 职场文书
市场部经理岗位职责
2014/04/10 职场文书
技术岗位竞聘演讲稿
2014/05/16 职场文书
公司老总年会致辞
2015/07/30 职场文书
2016年习总书记讲话学习心得体会
2016/01/20 职场文书
Oracle 数据仓库ETL技术之多表插入语句的示例详解
2021/04/12 Oracle