PHP SPL库
SPL,PHP 标准库(Standard PHP Library) ,此从 PHP 5.0 起内置的组件和接口,并且从 PHP5.3 已逐渐的成熟。SPL 其实在所有的 PHP5 开发环境中被内置,同时无需任何设置。
一.spl_autoload_register
更加方便的惰性加载
二.Iterator:迭代器
迭代器是比较重要的一种设计模式.普遍用于数组遍历.php的Iterator接口,预定义了以下方法:
Iterator extends Traversable {
/* 方法 */
abstract public mixed current ( void )
abstract public scalar key ( void )
abstract public void next ( void )
abstract public void rewind ( void )
abstract public boolean valid ( void )
}
任何类只要实现了该接口,则该类就可以迭代:
示例代码:
class Users implements Iterator
{
protected $position = 0;
protected $storage = array(); public function __construct()
{
$this->getWithData();
} public function getWithData()
{
$this->storage[] = array(
array('id' => 1, 'name' => 'john'),
array('id' => 2, 'name' => 'jake')
);
} public function current()
{
return $this->storage[$this->position];
} public function next()
{
$this->position ++;
} public function key()
{
return $this->position;
} public function valid()
{
return isset($this->storage[$this->position]);
} public function rewind()
{
$this->position = 0;
} } $users = new Users();
foreach ($users as $key => $value) {
echo $key;
var_dump($value);
} $users->rewind(); // 或者
while ($users->valid()) {
$cur = $users->current();
$users->next();
var_dump($cur);
}
SPL内置的迭代器类,可以方便的实现一些功能,比如迭代一个文件夹:
RecursiveIterator
RecursiveIteratorIterator
OuterIterator
IteratorIterator
FilterIterator
RecursiveFilterIterator
ParentIterator
SeekableIterator
LimitIterator
GlobIterator
CachingIterator
RecursiveCachingIterator
NoRewindIterator
AppendIterator
RecursiveIteratorIterator
InfiniteIterator
RegexIterator
RecursiveRegexIterator
EmptyIterator
RecursiveTreeIterator
ArrayIterator
ps:
注意区分ArrayIterator和ArrayAccess的区别;
ArrayAccess:提供像访问数组一样访问对象的能力的接口
三.ArrayAccess接口:
ArrayAccess {
/* 方法 */
abstract public boolean offsetExists ( mixed $offset )
abstract public mixed offsetGet ( mixed $offset )
abstract public void offsetSet ( mixed $offset , mixed $value )
abstract public void offsetUnset ( mixed $offset )
}
示例代码(结合单例模式):
class Config implements ArrayAccess
{
private static $instance = null;
private $configs; private function __construct()
{
$this->configs = array(
'db_type' => 'mysql',
'db_user' => 'root'
);
} public static function getInstance()
{
if (is_null(self::$instance)) {
self::$instance = new Config();
}
return self::$instance;
} public function offsetExists($offset)
{
return isset($this->configs[$offset]);
} public function offsetGet($offset)
{
return $this->configs[$offset];
} public function offsetSet($offset, $value)
{
$this->configs[$offset] = $value;
} public function offsetUnset($offset)
{
unset($this->configs[$offset]);
} public function __toString()
{
return (string)var_export($this->configs, true);
} private function __clone()
{
}
} $config = Config::getInstance();
print $config['db_type'];
print "\n";
$config['db_pwd'] = '1123';
print $config;
四.SplFixedArray
SplFixedArray 实例化一个固定长度的数组.SplFixedArray数组相比标准的PHP数组更接近于C语言的数组,而且由于splFixedArray没有使用散列(Hash)存储方式,因此效率更高.
PHP SPL库的更多相关文章
- 在PHP中使用SPL库中的对象方法进行XML与数组的转换
虽说现在很多的服务提供商都会提供 JSON 接口供我们使用,但是,还是有不少的服务依然必须使用 XML 作为接口格式,这就需要我们来对 XML 格式的数据进行解析转换.而 PHP 中并没有像 json ...
- php spl库的使用(PHP标准库)【摘抄引用】
文章来源与推荐阅读:阮一峰--PHP SPL笔记 && PHP SPL使用方法和他的威力 1.SPL 是什么? SPL:standard php library php标准库,此 ...
- php—Spl库常用数据结构基本用法
数据结构之一 : 栈 //zhan $stack = new SplStack(); $stack->push('data1'); $stack->push('data2'); echo ...
- PHP 设计模式 笔记与总结(3)SPL 标准库
SPL 库的使用(PHP 标准库) 1. SplStack,SplQueue,SplHeap,SplFixedArray 等数据结构类 ① 栈(SplStack)(先进后出的数据结构) index.p ...
- 深入浅出 PHP SPL(PHP 标准库)(转)
一.什么是spl库? SPL是用于解决典型问题(standard problems)的一组接口与类的集合. 此扩展只能在php 5.0以后使用,从PHP 5.3.0 不再被关闭,会一直有效.成为php ...
- PHP的SPL扩展库(一)数据结构
SPL 库也叫做 PHP 标准库,主要就是用于解决典型问题的一组接口或类的集合.这些典型问题包括什么呢?比如我们今天要讲的数据结构,还有一些设计模式的实现,就像我们之前讲过的观察者模式相关的接口在 S ...
- Arduino库和STM32的寄存器、标准库、HAL库、LL库开发比较之GPIO
标题: Arduino库和STM32的寄存器.标准库.HAL库.LL库开发比较之GPIO 作者: 梦幻之心星 sky-seeker@qq.com 标签: [#Arduino,#STM32,#库,#开发 ...
- (转)yii流程,入口文件下的准备工作
yii流程 一 目录文件 |-framework 框架核心库 |--base 底层类库文件夹,包含CApplication(应用类,负责全局的用户请求处理,它管理的应用组件集, ...
- PHP全栈工程师学习大纲
一.高性能网站开发功力提升 时间 标题 内容概要 2015-12-28 开学典礼以及工程师成长路线图 工程师成长的发展路径图.三个阶段,在各个阶段需要提升自己的地方,从技术上也讲了一些提高分析代码的工 ...
随机推荐
- 倒置函数reverse的用法
倒置字符串函数reverse:用于倒置字符串s中的各个字符的位置,如原来字符串中如果初始值为123456,则通过reverse函数可将其倒置为654321,程序如下:#include<stdio ...
- http://stormzhang.com/opensource/2016/06/26/android-open-source-project-recommend1/
转载自:http://stormzhang.com/opensource/2016/06/26/android-open-source-project-recommend1/ 推荐他的所有博文~ 图片 ...
- [转载]EasyUI中数据表格DataGrid添加排序功能
我们这里演示的是EasyUI数据表格DataGrid从服务器端排序功能,因为觉的本地数据排序没有多大的作用,一般我们DataGrid不会读取全部数据,只会读取当前页的数据,所以本地数据排序也只是对当前 ...
- OpenGL入门学习(五)
http://developer.178.com/201103/94955548786.html 今天要讲的是三维变换的内容,课程比较枯燥.主要是因为很多函数在单独使用时都不好描述其效果,我只好在最后 ...
- system call hooking 系统调用增加或劫持
1. 引言:这篇文章提供了一种增加自定义系统调用或劫持原有的系统调用的实现方法,只针对 linux 系统.主要思路是获取系统调用表 sys_call_table 地址,然后用新函数地址覆盖系统调用表某 ...
- 多个类的DLL封装及调用
#define FaceLIBDLL #include "stdafx.h" #include "facedll.h" #include <opencv2 ...
- Java错误随手记
一.Eclipse启动时提示: An internal error occurred during: "Initializing Java Tooling" 1.关闭Eclipse ...
- 1.tornado基础
import tornado.web ''' tornado基础web框架模块 ''' import tornado.ioloop ''' tornado的核心循环IO模块,封装了linux的epol ...
- 简述web工程师的职责与学习
最近两年web突然很火,也有很多人涌入这一行,但这行实际上是进来的人很多,出去的人也很多. 在我眼里,Web前端开发工程师的职责有:1.Web前端表现层及与前后端交互的架构设计和开发2.配合后台开发人 ...
- [BZOJ1069][SCOI2007]最大土地面积 凸包+旋转卡壳
1069: [SCOI2007]最大土地面积 Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 3669 Solved: 1451[Submit][Sta ...