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库的更多相关文章

  1. 在PHP中使用SPL库中的对象方法进行XML与数组的转换

    虽说现在很多的服务提供商都会提供 JSON 接口供我们使用,但是,还是有不少的服务依然必须使用 XML 作为接口格式,这就需要我们来对 XML 格式的数据进行解析转换.而 PHP 中并没有像 json ...

  2. php spl库的使用(PHP标准库)【摘抄引用】

    文章来源与推荐阅读:阮一峰--PHP SPL笔记  &&  PHP SPL使用方法和他的威力 1.SPL 是什么? SPL:standard php library php标准库,此 ...

  3. php—Spl库常用数据结构基本用法

    数据结构之一 : 栈 //zhan $stack = new SplStack(); $stack->push('data1'); $stack->push('data2'); echo ...

  4. PHP 设计模式 笔记与总结(3)SPL 标准库

    SPL 库的使用(PHP 标准库) 1. SplStack,SplQueue,SplHeap,SplFixedArray 等数据结构类 ① 栈(SplStack)(先进后出的数据结构) index.p ...

  5. 深入浅出 PHP SPL(PHP 标准库)(转)

    一.什么是spl库? SPL是用于解决典型问题(standard problems)的一组接口与类的集合. 此扩展只能在php 5.0以后使用,从PHP 5.3.0 不再被关闭,会一直有效.成为php ...

  6. PHP的SPL扩展库(一)数据结构

    SPL 库也叫做 PHP 标准库,主要就是用于解决典型问题的一组接口或类的集合.这些典型问题包括什么呢?比如我们今天要讲的数据结构,还有一些设计模式的实现,就像我们之前讲过的观察者模式相关的接口在 S ...

  7. Arduino库和STM32的寄存器、标准库、HAL库、LL库开发比较之GPIO

    标题: Arduino库和STM32的寄存器.标准库.HAL库.LL库开发比较之GPIO 作者: 梦幻之心星 sky-seeker@qq.com 标签: [#Arduino,#STM32,#库,#开发 ...

  8. (转)yii流程,入口文件下的准备工作

    yii流程 一 目录文件 |-framework     框架核心库 |--base         底层类库文件夹,包含CApplication(应用类,负责全局的用户请求处理,它管理的应用组件集, ...

  9. PHP全栈工程师学习大纲

    一.高性能网站开发功力提升 时间 标题 内容概要 2015-12-28 开学典礼以及工程师成长路线图 工程师成长的发展路径图.三个阶段,在各个阶段需要提升自己的地方,从技术上也讲了一些提高分析代码的工 ...

随机推荐

  1. 分享C# GC 非原创

    一. 托管资源的分配 CLR在运行时管理着一段内存地址空间(虚拟地址空间,在运行中会映射到物理内存地址中),分为“托管堆”和“栈”两部分,栈用于存储值类型数据,它会在方法执行结束后自动销毁其中引用的值 ...

  2. 51nod 1273 旅行计划——思维题

    某个国家有N个城市,编号0 至 N-1,他们之间用N - 1条道路连接,道路是双向行驶的,沿着道路你可以到达任何一个城市.你有一个旅行计划,这个计划是从编号K的城市出发,每天到达一个你没有去过的城市, ...

  3. linux.backspace乱码(转)

    42 linux.backspace乱码 linux环境sqlplus中使用backspace键出现乱码的解决方法2008-04-30 16:32 在linux环境下使用sqlplus,在回删(bac ...

  4. nginx+lua_module安装

    1.LuaJit安装 # cd /usr/local/src # git clone http://luajit.org/git/luajit-2.0.git # cd luajit-2.0 # ma ...

  5. 条件变量(Condition Variable)详解

    条件变量(Condtion Variable)是在多线程程序中用来实现“等待->唤醒”逻辑常用的方法.举个简单的例子,应用程序A中包含两个线程t1和t2.t1需要在bool变量test_cond ...

  6. poj 3164(最小树形图模板)

    题目链接:http://poj.org/problem?id=3164 详细可以看这里:http://www.cnblogs.com/vongang/archive/2012/07/18/259685 ...

  7. Jquery实现逐屏加载图片

    引用jquery.scrollLoading.js $(document).ready(function () { //实现图片慢慢浮现出来的效果 $("img").load(fu ...

  8. MSSQL纵列转横列

    在工作中我们一般会遇到将纵列转横列的需求,具体代码: 1.建表 CREATE TABLE [dbo].[AcrossChangeEndLong]( ,) NOT NULL, ) NOT NULL, ) ...

  9. HDU 1024 Max Sum Plus Plus(基础dp)

    Max Sum Plus Plus Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others ...

  10. ELK集群

    kafka集群-------------------1. 下载wget http://mirror.rise.ph/apache/kafka/0.11.0.0/kafka_2.12-0.11.0.0. ...