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. 关于Bootstrap 利用radio实现tab切换的一个问题

    1.html代码 <div class="col-sm-10 nav nav-tabs" id="typelist" role="tablist ...

  2. 行为型设计模式之迭代器模式(Iterator)

    结构 意图 提供一种方法顺序访问一个聚合对象中各个元素, 而又不需暴露该对象的内部表示. 适用性 访问一个聚合对象的内容而无需暴露它的内部表示. 支持对聚合对象的多种遍历. 为遍历不同的聚合结构提供一 ...

  3. 跳石头(NOIP2015) (二分查找)

    原题传送门 好久没更了..昨天去学zkw线段树,被zxyer狠狠地D了一顿.. 来补坑.. 这是一道很奇特的题目. 根据题目可以看出这道题有二分题具有的性质.. 不懂二分性质的可以看我以前的博客 传送 ...

  4. Linux将命令添加到PATH中【转】

    转自:http://www.jb51.net/LINUXjishu/150167.html 电脑中必不可少的就是操作系统.而Linux的发展非常迅速,有赶超微软的趋势.这里介绍Linux的知识,让你学 ...

  5. LPTSTR\LPCTSTR\LPWSTR\LPCWSTR 字母的意思 及 区别

    标签: 杂谈 分类: VC     char*   是指向ANSI字符数组的指针,其中每个字符占据8位(有效数据是除掉最高位的其他7位),这里保持了与传统的C,C++的兼容.        LP的含义 ...

  6. BufferedInputStream&BufferedOutputStream

    使用字符缓冲区相关实现copy文件: public static void main(String[] args) { //创建文件对象指定要拷贝的文件路径(源文件),文件须存在,测试用例不做判断 F ...

  7. python--cProfile,memory_profiler,psutil

    关于测试代码用了多长时间,我们之前介绍了timeit.相较于timeit,python中还有一个更加强大的模块,cProfile模块 (提到cProfile,其实还有一个profile,但profil ...

  8. ZOJ2112 Dynamic Rankings(整体二分)

    今天学习了一个奇技淫巧--整体二分.关于整体二分的一些理论性的东西,可以参见XRH的<浅谈数据结构题的几个非经典解法>.然后下面是一些个人的心得体会吧,写下来希望加深一下自己的理解,或者如 ...

  9. Visual Studio跨平台开发(1):Hello Xamarin!

    前言 应用程序发展的脚步, 从来没有停过. 从早期的Windows 应用程序, 到网络时代的web 应用程序, 再到近几年相当盛行的行动装置应用程序(Mobile Application), 身为C# ...

  10. [thinkphp] MD!! 数组构造的好好的,硬是有一个值无法写入数据库

    我都要抓狂了,buildsql()方法又用不了,最后决定看runtime里面的文件.先删掉所有的runtime,然后提交一次,就可以在runtime里面看到对应解析后的文件,这样应该可以知道问题在哪. ...