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 相关文章推荐
libmysql.dll与php.ini是否真的要拷贝到c:\windows目录下呢
Mar 15 PHP
Windows 下的 PHP-PEAR 安装方法
Nov 20 PHP
解析curl提交GET,POST,Cookie的简单方法
Jun 29 PHP
Windows下的PHP 5.3.x安装 Zend Guard Loader教程
Sep 06 PHP
为PHP5.4开启Zend OPCode缓存
Dec 26 PHP
Yii中创建自己的Widget实例
Jan 05 PHP
对比分析php中Cookie与Session的异同
Feb 19 PHP
PHP获取当前URL路径的处理方法(适用于多条件筛选列表)
Feb 10 PHP
Laravel中encrypt和decrypt的实现方法
Sep 24 PHP
YII框架http缓存操作示例
Apr 29 PHP
PHP实现百度人脸识别
May 06 PHP
Yii框架where查询用法实例分析
Oct 22 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
WIN98下Apache1.3.14+PHP4.0.4的安装
2006/10/09 PHP
比较时间段一与时间段二是否有交集的php函数
2011/05/31 PHP
PHP魔术方法以及关于独立实例与相连实例的全面讲解
2016/10/18 PHP
Yii框架用户登录session丢失问题解决方法
2017/01/07 PHP
PHP实现数组转JSon和JSon转数组的方法示例
2018/06/14 PHP
js获取url参数的使用扩展实例
2007/12/29 Javascript
XMLHttpRequest处理xml格式的返回数据(示例代码)
2013/11/21 Javascript
js中setTimeout()与clearTimeout()用法实例浅析
2015/05/12 Javascript
js密码强度检测
2016/01/07 Javascript
基于javascript html5实现多文件上传
2016/03/03 Javascript
js实现图片切换(动画版)
2016/12/25 Javascript
利用transition实现文字上下抖动的效果
2017/01/21 Javascript
js 事件的传播机制(实例讲解)
2017/07/20 Javascript
jQuery实现下拉菜单动态添加数据点击滑出收起其他功能
2018/06/14 jQuery
nodejs读取本地中文json文件出现乱码解决方法
2018/10/10 NodeJs
如何让Nodejs支持H5 History模式(connect-history-api-fallback源码分析)
2019/05/30 NodeJs
基于vue、react实现倒计时效果
2019/08/26 Javascript
vue-cli设置css不生效的解决方法
2020/02/07 Javascript
解决vue.js中settimeout遇到的问题(时间参数短效果不稳定)
2020/07/21 Javascript
Vue中component标签解决项目组件化操作
2020/09/04 Javascript
详解python之配置日志的几种方式
2017/05/22 Python
使用python编写监听端
2018/04/12 Python
使用pip安装python库的多种方式
2019/07/31 Python
python实现的接收邮件功能示例【基于网易POP3服务器】
2019/09/11 Python
pycharm安装及如何导入numpy
2020/04/03 Python
详解Django ORM引发的数据库N+1性能问题
2020/10/12 Python
纯CSS3制作的鼠标悬停时边框旋转
2017/01/03 HTML / CSS
DHC美国官网:日本通信销售第一的化妆品品牌
2017/11/12 全球购物
JAVA中的关键字有什么特点
2014/03/07 面试题
2014财产信托协议书范本
2014/11/18 职场文书
2015年社会治安综合治理工作总结
2015/04/10 职场文书
2015年基建工作总结范文
2015/05/23 职场文书
小学教师暑期培训心得体会
2016/01/09 职场文书
干部外出学习心得体会
2016/01/18 职场文书
C站最全Python标准库总结,你想要的都在这里
2021/07/03 Python
Win11怎么添加用户?Win11添加用户账户的方法
2022/07/15 数码科技