本文实例分析了PHP中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();?>

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指向这两个不同的函数地址来实现不同的自动加载机制。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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中spl_autoload_register()函数用法实例详解的更多相关文章

  1. python中argparse模块用法实例详解

    python中argparse模块用法实例详解 这篇文章主要介绍了python中argparse模块用法,以实例形式较为详细的分析了argparse模块解析命令行参数的使用技巧,需要的朋友可以参考下 ...

  2. jQuery中on()方法用法实例详解

    这篇文章主要介绍了jQuery中on()方法用法,实例分析了on()方法的功能及各种常见的使用技巧,并对比分析了与bind(),live(),delegate()等方法的区别,需要的朋友可以参考下 本 ...

  3. jquery中attr()与prop()函数用法实例详解(附用法区别)

    本文实例讲述了jQuery中attr()与prop()函数用法.分享给大家供大家参考,具体如下: 一.jQuery的attr()方法 jquery中用attr()方法来获取和设置元素属性,attr是a ...

  4. Python回调函数用法实例详解

    本文实例讲述了Python回调函数用法.分享给大家供大家参考.具体分析如下: 一.百度百科上对回调函数的解释: 回调函数就是一个通过函数指针调用的函数.如果你把函数的指针(地址)作为参数传递给另一个函 ...

  5. Laravel中Trait的用法实例详解

    本文实例讲述了Laravel中Trait的用法.分享给大家供大家参考,具体如下: 看看PHP官方手册对Trait的定义: 自 PHP 5.4.0 起,PHP 实现了代码复用的一个方法,称为 trait ...

  6. PHP中 spl_autoload_register() 函数用法

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

  7. Nmap在实战中的高级用法(详解)

    @ 目录 Nmap在实战中的高级用法(详解) Nmap简单的扫描方式: 一.Nmap高级选项 1.查看本地路由与接口 2.指定网口与IP地址 3.定制探测包 二.Nmap扫描防火墙 1.SYN扫描 2 ...

  8. JS中的函数节流throttle详解和优化

    JS中的函数节流throttle详解和优化在前端开发中,有时会为页面绑定resize事件,或者为一个页面元素绑定拖拽事件(mousemove),这种事件有一个特点,在一个正常的操作中,有可能在一个短的 ...

  9. Oracle排名函数(Rank)实例详解

    这篇文章主要介绍了Oracle排名函数(Rank)实例详解,需要的朋友可以参考下     --已知:两种排名方式(分区和不分区):使用和不使用partition --两种计算方式(连续,不连续),对应 ...

随机推荐

  1. Android_内部存储文件的读写

    内部存储文件即raw和assets项目文件夹下的文件,项目卸载时被删除. 四种文件操作模式 文件存储: public void save(String filename, String filecon ...

  2. python3包、模块、类、方法的认识

    包>>模块>>类>> 函数 包:就是一个目录,import time from+import导入包中的部分模块 直接到类 from budaoguan.common ...

  3. windows10 找回windows照片查看器的方法

    突然发现windows10自带的图片查看器打开预览查看速度还是可以的,但是却找不到了,,,,, 下面就是如何找回 windows 图片查看器的操作了,只需要运行一个bat程序即可!!!!!! 随便新建 ...

  4. 为什么 MySQL 使用 B+ 树

    为什么 MySQL 使用 B+ 树是面试中经常会出现的问题,很多人对于这个问题可能都有一些自己的理解,但是多数的回答都不够完整和准确,大多数人都只会简单说一下 B+ 树和 B 树的区别,但是都没有真正 ...

  5. 在vue项目中设置BASE_URL

    在vue项目中设置BASE_URL 1.在config文件夹中新建global.js文件 const BASE_URL = 'http://192.168.1.62:8080/rest/' expor ...

  6. C# Enumerable

    使用Enumerable.Range 打印数字0到9 static void Main(string[] args) { , ); //Range方法获取两个int参数:一个起始数,一个是要生成的结果 ...

  7. arm学习笔记

    学习ARM也有一定时间了,想想还是记点东西,要不以后就忘了.这是我的第一片,简简单单.但比较基础.1. ARM中一些常见英文缩写解释MSB:最高有效位:LSB:最低有效位:AHB:先进的高性能总线:V ...

  8. [转]触发fullgc的条件

    良好的状态是:minor gc比较多 full gc 比较少 因为fullgc时间比较慢,而且会占用CPU的时间片. 不好的状态是:minor gc比较少 full gc 比较多 这样程序就一直卡在f ...

  9. Selenium原理

      from selenium import webdriver:导入webdriver模块 当导入webdriver模块时,会执行\selenium\webdriver目录下的__init__.py ...

  10. 2.4测试赛AC代码临时保存

    //H #include<cstdio> #include<cstdlib> #include<cstring> #include<stack> usi ...