PHP SPL标准库中的常用函数介绍


Posted in PHP onMay 11, 2015

PHP SPL标准库中提供了一些函数用来处理如自动加载、迭代器处理等。

spl_autoload_extensions()添加spl_autoload()可加载的文件扩展名
spl_autoload_register()注册函数到SPL __autoload函数栈中。

/*test1.php*/

<?php

class Test1

{

}

 

/*test2.lib.php*/

<?php

class Test2

{

}

 

/*test.php*/

<?php

//设置可加载类的文件扩展名

spl_autoload_extensions(".php,.inc.php,.class.php,.lib.php");

//设置include_path,autoload会在这些path中去寻找类文件,可通过PATH_SEPARATOR添加多个path

set_include_path(get_include_path().PATH_SEPARATOR.'libs/');

//不提供参数,默认实现函数是spl_autoload()

spl_autoload_register();

 

$test1 = new Test1();

$test2 = new Test2();

spl_autoload()它是__autoload()的默认实现,它会去include_path中加载文件(.php/.inc)

/*test1.php*/

<?php

class Test1

{

}

 

/*test.php*/

<?php

set_include_path(get_include_path().PATH_SEPARATOR.'libs/');

spl_autoload('test1');

$test1 = new Test1();

spl_autoload_call()调用所有spl_autoload_register注册函数来加载文件

/*test1.php*/

<?php

class Test

{

    public function getFilename()

    {

        echo 'test1.php';

    }

}

 

/*test2.lib.php*/

<?php

class Test

{

    public function getFilename()

    {

        echo 'test2.lib.php';

    }

}

 

/*test.php*/

<?php

 

function loader($classname)

{

    if($classname == 'Test1') {

        require __DIR__ . '/test1.php';

    }

    if($classname == 'Test2') {

        require __DIR__ . '/test2.lib.php';

    }

}

 

spl_autoload_register('loader');

spl_autoload_call('Test2');

 

 

$test = new Test();

$test->getFilename(); //test2.lib.php

其它SPL 函数介绍:
class_implements — 返回指定的类实现的所有接口。
class_parents — 返回指定类的父类。
class_uses — Return the traits used by the given class
iterator_apply — 为迭代器中每个元素调用一个用户自定义函数
iterator_count — 计算迭代器中元素的个数
iterator_to_array — 将迭代器中的元素拷贝到数组
spl_autoload_functions — 返回所有已注册的__autoload()函数
spl_autoload_unregister — 注销已注册的__autoload()函数
spl_classes — 返回所有可用的SPL类
spl_object_hash — 返回指定对象的hash id

如iterator相关函数使用:

$iterator  = new  ArrayIterator (array( 'recipe' => 'pancakes' ,  'egg' ,  'milk' ,  'flour' ));

 

print_r(iterator_to_array($iterator)); //将迭代器元素转化为数组

echo iterator_count($iterator); //计算迭代器元素的个数

print_r(iterator_apply($iterator, 'print_item', array($iterator)));//为迭代器每个元素调用自定义函数

 

 

function print_item(Iterator $iterator)

{

    echo  strtoupper ( $iterator -> current ()) .  "\n" ;

    return  TRUE ;

}
PHP 相关文章推荐
十天学会php之第二天
Oct 09 PHP
全文搜索和替换
Oct 09 PHP
PHP整数取余返回负数的相关解决方法
May 15 PHP
php针对cookie操作的队列操作类实例
Dec 10 PHP
php控制文件下载速度的方法
Mar 24 PHP
Yii框架上传图片用法总结
Mar 28 PHP
php使用函数pathinfo()、parse_url()和basename()解析URL
Nov 25 PHP
Yii2 中实现单点登录的方法
Mar 09 PHP
laravel 数据迁移与 Eloquent ORM的实现方法
Apr 12 PHP
Yii框架日志操作图文与实例详解
Sep 09 PHP
PHP实现简单的协程任务调度demo示例
Feb 01 PHP
php慢查询日志和错误日志使用详解
Feb 27 PHP
PHP中的类型约束介绍
May 11 #PHP
PHP SPL标准库之接口(Interface)详解
May 11 #PHP
PHP SPL标准库之文件操作(SplFileInfo和SplFileObject)实例
May 11 #PHP
PHP设计模式之适配器模式代码实例
May 11 #PHP
Mac环境下php操作mysql数据库的方法分享
May 11 #PHP
PHP设计模式之装饰者模式代码实例
May 11 #PHP
PHP超牛逼无限极分类生成树方法
May 11 #PHP
You might like
不用GD库生成当前时间的PNG格式图象的程序
2006/10/09 PHP
thinkphp模板赋值与替换实例简述
2014/11/24 PHP
CI框架常用函数封装实例
2016/11/21 PHP
超级简单的图片防盗(HTML),好用
2007/04/08 Javascript
js控制表单操作的常用代码小结
2013/08/15 Javascript
jQuery中prevUntil()方法用法实例
2015/01/08 Javascript
仿JQuery输写高效JSLite代码的一些技巧
2015/01/13 Javascript
JS简单模拟触发按钮点击功能的方法
2015/11/30 Javascript
BootStrap selectpicker
2016/06/20 Javascript
JS ES6中setTimeout函数的执行上下文示例
2017/04/27 Javascript
基于node.js制作简单爬虫教程
2017/06/29 Javascript
JS实现动态生成html table表格的方法分析
2018/07/11 Javascript
layui 给数据表格加序号的方法
2018/08/20 Javascript
[01:11:08]Winstrike vs NB 2018国际邀请赛淘汰赛BO1 8.21
2018/08/22 DOTA
python基础教程之python消息摘要算法使用示例
2014/02/10 Python
Python动态加载模块的3种方法
2014/11/22 Python
Python IDE PyCharm的基本快捷键和配置简介
2015/11/04 Python
python中文件变化监控示例(watchdog)
2017/10/16 Python
全面分析Python的优点和缺点
2018/02/07 Python
python Spyder界面无法打开的解决方法
2018/04/27 Python
python 分离文件名和路径以及分离文件名和后缀的方法
2018/10/21 Python
python 标准差计算的实现(std)
2019/07/29 Python
使用Keras中的ImageDataGenerator进行批次读图方式
2020/06/17 Python
HTML5中判断横屏竖屏的方法(移动端)
2016/08/04 HTML / CSS
英国第一豪华护肤品牌:Elemis
2017/10/12 全球购物
英国高街奥特莱斯:Highstreet Outlet
2019/11/21 全球购物
C#公司笔试题
2014/03/28 面试题
Ajax的工作原理
2015/12/04 面试题
实习教师自我鉴定
2013/12/12 职场文书
小学生常见病防治方案
2014/06/06 职场文书
学习党的群众路线实践活动思想汇报
2014/09/12 职场文书
房贷工资证明范本
2015/06/12 职场文书
毕业赠语大全
2015/06/23 职场文书
Python基础之赋值,浅拷贝,深拷贝的区别
2021/04/30 Python
Python实现日志实时监测的示例详解
2022/04/06 Python
Redis+AOP+自定义注解实现限流
2022/06/28 Redis