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. 51nod1086 背包问题 V2——二进制优化

    有N种物品,每种物品的数量为C1,C2......Cn.从中任选若干件放在容量为W的背包里,每种物品的体积为W1,W2......Wn(Wi为整数),与之相对应的价值为P1,P2......Pn(Pi ...

  2. OutputDebugString方便格式化WIN32封装

    void TRACE(LPCTSTR lpszFmt, ...) { va_list args; va_start(args, lpszFmt); ; TCHAR *lpszBuf = (TCHAR* ...

  3. wsgi的学习(2):uWSGI的概念

    uWSGI是一个Web服务器,它实现了WSGI协议.uwsgi.http等协议.Nginx中HttpUwsgiModule的作用是与uWSGI服务器进行交换. 要注意 WSGI / uwsgi / u ...

  4. c++ set容器排序准则

    转载两篇博客: http://blog.csdn.net/lishuhuakai/article/details/51404214 http://blog.csdn.net/lihao21/artic ...

  5. springBoot Feign

    1.引入依赖包 <!-- 引入关于 eureka-server的依赖 --> <dependency> <groupId>org.springframework.c ...

  6. 模拟【p2239】 螺旋矩阵

    顾z 你没有发现两个字里的blog都不一样嘛 qwq 题目描述--->p2239 螺旋矩阵 看到题,很明显,如果直接模拟的话,复杂度为\(O(n^2)\)过不去.(这个复杂度应该不正确,我不会分 ...

  7. 后门构建工具Backdoor Factory

    后门构建工具Backdoor Factory   在渗透测试中,后门程序帮助渗透测试人员在目标机器上执行各种预期的操作.例如,它可以建立从目标主机到攻击机的网络连接,方便渗透测试人员控制目标主机.Ka ...

  8. 将一个txt里的A和B谈话内容获取出来并分别保存到A和B的txt文件中

    import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream;import java.io.Fi ...

  9. 新博客:11101001.com

    开了一个新blog 但还是会用这个写博客 新博客地址11101001.com

  10. 四. Java继承和多态2. Java super关键字

    super 关键字与 this 类似,this 用来表示当前类的实例,super 用来表示父类. super 可以用在子类中,通过点号(.)来获取父类的成员变量和方法.super 也可以用在子类的子类 ...