一、什么是spl库?

SPL是用于解决典型问题(standard problems)的一组接口与类的集合。

此扩展只能在php 5.0以后使用,从PHP 5.3.0 不再被关闭,会一直有效.成为php内核组件一部份。

SPL提供了一组标准数据结构。

二、SPL如何使用?

1.构建此扩展不需要其他扩展。

更详细的情况可参考 http://php.net/manual/zh/spl.datastructures.php

双向链表

双链表是一种重要的线性存储结构,对于双链表中的每个节点,不仅仅存储自己的信息,还要保存前驱和后继节点的地址。

SplDoublyLinkedList

SplDoublyLinkedList implements Iterator , ArrayAccess , Countable {    /* 方法 */
public __construct ( void )
public void add ( mixed $index , mixed $newval )
public mixed bottom ( void )//双链表的尾部节点
public int count ( void )//双联表元素的个数
public mixed current ( void )//当前记录
public int getIteratorMode ( void ) //获取迭代模式
public bool isEmpty ( void )//检测双链表是否为空
public mixed key ( void )//当前节点索引
public void next ( void )//移到下条记录
public bool offsetExists ( mixed $index )//指定index处节点是否存在
public mixed offsetGet ( mixed $index )//获取指定index处节点值
public void offsetSet ( mixed $index , mixed $newval )//设置指定index处值
public void offsetUnset ( mixed $index )//删除指定index处节点
public mixed pop ( void )//从双链表的尾部弹出元素
public void prev ( void )//移到上条记录
public void push ( mixed $value )//添加元素到双链表的尾部
public void rewind ( void )//将指针指向迭代开始处
public string serialize ( void )//序列化存储
public void setIteratorMode ( int $mode )//设置迭代模式
public mixed shift ( void )//双链表的头部移除元素
public mixed top ( void )//双链表的头部节点
public void unserialize ( string $serialized )//反序列化
public void unshift ( mixed $value )//双链表的头部添加元素
public bool valid ( void )//检查双链表是否还有节点
}

接下来是使用方法:

$list = new SplDoublyLinkedList();
$list->push('a');
$list->push('b');
$list->push('c');
$list->push('d'); $list->unshift('top');
$list->shift(); $list->rewind();//rewind操作用于把节点指针指向Bottom所在的节点
echo 'curren node:'.$list->current()."<br />";//获取当前节点 $list->next();//指针指向下一个节点
echo 'next node:'.$list->current()."<br />"; $list->next();
$list->next();
$list->prev();//指针指向上一个节点
echo 'next node:'.$list->current()."<br />"; if($list->current())
echo 'current node is valid<br />';
else
echo 'current node is invalid<br />'; if($list->valid())//如果当前节点是有效节点,valid返回true
echo "valid list<br />";
else
echo "invalid list <br />"; var_dump(array(
'pop' => $list->pop(),
'count' => $list->count(),
'isEmpty' => $list->isEmpty(),
'bottom' => $list->bottom(),
'top' => $list->top()
)); $list->setIteratorMode(SplDoublyLinkedList::IT_MODE_FIFO);
var_dump($list->getIteratorMode()); for($list->rewind(); $list->valid(); $list->next()){
echo $list->current().PHP_EOL;
} var_dump($a = $list->serialize());
//print_r($list->unserialize($a)); $list->offsetSet(0,'new one');
$list->offsetUnset(0);
var_dump(array(
'offsetExists' => $list->offsetExists(4),
'offsetGet' => $list->offsetGet(0),
));
var_dump($list); //堆栈,先进后出
$stack = new SplStack();//继承自SplDoublyLinkedList类 $stack->push("a<br />");
$stack->push("b<br />"); echo $stack->pop();
echo $stack->pop();
echo $stack->offsetSet(0,'B');//堆栈的offset=0是Top所在的位置,offset=1是Top位置节点靠近bottom位置的相邻节点,以此类推 $stack->rewind();//双向链表的rewind和堆栈的rewind相反,堆栈的rewind使得当前指针指向Top所在的位置,而双向链表调用之后指向bottom所在位置
echo 'current:'.$stack->current().'<br />';
$stack->next();//堆栈的next操作使指针指向靠近bottom位置的下一个节点,而双向链表是靠近top的下一个节点
echo 'current:'.$stack->current().'<br />';
echo '<br /><br />'; //队列,先进先出
$queue = new SplQueue();//继承自SplDoublyLinkedList类
$queue->enqueue("a<br />");//插入一个节点到队列里面的Top位置
$queue->enqueue("b<br />");
$queue->offsetSet(0,'A');//堆栈的offset=0是Top所在的位置,offset=1是Top位置节点靠近bottom位置的相邻节点,以此类推
echo $queue->dequeue();
echo $queue->dequeue();
echo "<br /><br />";

堆(Heap)就是为了实现优先队列而设计的一种数据结构,它是通过构造二叉堆(二叉树的一种)实现。根节点最大的堆叫做最大堆或大根堆(SplMaxHeap),根节点最小的堆叫做最小堆或小根堆(SplMinHeap)。二叉堆还常用于排序(堆排序)

abstract SplHeap implements Iterator , Countable {
/* 方法 用法同双向链表一致 */
public __construct ( void )
abstract protected int compare ( mixed $value1 , mixed $value2 )
public int count ( void )
public mixed current ( void )
public mixed extract ( void )
public void insert ( mixed $value )
public bool isEmpty ( void )
public mixed key ( void )
public void next ( void )
public void recoverFromCorruption ( void )
public void rewind ( void )
public mixed top ( void )
public bool valid ( void )
}

使用方法:

//堆
class MySplHeap extends SplHeap{
//compare()方法用来比较两个元素的大小,绝对他们在堆中的位置
public function compare( $value1, $value2 ) {
return ( $value1 - $value2 );
}
} $obj = new MySplHeap();
$obj->insert(0);
$obj->insert(1);
$obj->insert(2);
$obj->insert(3);
$obj->insert(4);
echo $obj->top();//
echo $obj->count();// foreach ($obj as $item) {
echo $item."<br />";
}

阵列

优先队列也是非常实用的一种数据结构,可以通过加权对值进行排序,由于排序在php内部实现,业务代码中将精简不少而且更高效。通过SplPriorityQueue::setExtractFlags(int  $flag)设置提取方式可以提取数据(等同最大堆)、优先级、和两者都提取的方式。

SplFixedArray implements Iterator , ArrayAccess , Countable {
  /* 方法 */  
  public __construct ([ int $size = 0 ] )
  public int count ( void )
  public mixed current ( void )
  public static SplFixedArray fromArray ( array $array [, bool $save_indexes = true ] )
  public int getSize ( void )
  public int key ( void )
  public void next ( void )
  public bool offsetExists ( int $index )
  public mixed offsetGet ( int $index )
  public void offsetSet ( int $index , mixed $newval )
  public void offsetUnset ( int $index )
  public void rewind ( void )
  public int setSize ( int $size )
  public array toArray ( void )
  public bool valid ( void )
  public void __wakeup ( void )
}

使用方法:

$arr = new SplFixedArray(4);
$arr[0] = 'php';
$arr[1] = 1;
$arr[3] = 'python';//遍历, $arr[2] 为null
foreach($arr as $v) {
echo $v . PHP_EOL;
} //获取数组长度
echo $arr->getSize(); //4 //增加数组长度
$arr->setSize(5);
$arr[4] = 'new one'; //捕获异常
try{
echo $arr[10];
} catch (RuntimeException $e) {
echo $e->getMessage();
}

映射

用来存储一组对象的,特别是当你需要唯一标识对象的时候。

SplObjectStorage implements Countable , Iterator , Serializable , ArrayAccess {
  /* 方法 */  
  public void addAll ( SplObjectStorage $storage )
  public void attach ( object $object [, mixed $data = NULL ] )
  public bool contains ( object $object )
  public int count ( void )
  public object current ( void )
  public void detach ( object $object )
  public string getHash ( object $object )
  public mixed getInfo ( void )
  public int key ( void )
  public void next ( void )
  public bool offsetExists ( object $object )
  public mixed offsetGet ( object $object )
  public void offsetSet ( object $object [, mixed $data = NULL ] )
  public void offsetUnset ( object $object )
  public void removeAll ( SplObjectStorage $storage )
  public void removeAllExcept ( SplObjectStorage $storage )
  public void rewind ( void )
  public string serialize ( void )
  public void setInfo ( mixed $data )
  public void unserialize ( string $serialized )
  public bool valid ( void )
}

使用方法:

class A {
public $i;
public function __construct($i) {
$this->i = $i;
}
} $a1 = new A(1);
$a2 = new A(2);
$a3 = new A(3);
$a4 = new A(4); $container = new SplObjectStorage(); //SplObjectStorage::attach 添加对象到Storage中
$container->attach($a1);
$container->attach($a2);
$container->attach($a3); //SplObjectStorage::detach 将对象从Storage中移除
$container->detach($a2); //SplObjectStorage::contains用于检查对象是否存在Storage中
var_dump($container->contains($a1)); //true
var_dump($container->contains($a4)); //false //遍历
$container->rewind();
while($container->valid()) {
var_dump($container->current());
$container->next();
}

原文地址:http://www.imooc.com/article/34286

深入浅出 PHP SPL(PHP 标准库)(转)的更多相关文章

  1. PHP SPL(PHP 标准库)

    一.什么是SPL? SPL是用于解决典型问题(standard problems)的一组接口与类的集合.(出自:http://php.net/manual/zh/intro.spl.php) SPL, ...

  2. 【夯实PHP基础】PHP标准库 SPL

    PHP SPL笔记 这几天,我在学习PHP语言中的SPL. 这个东西应该属于PHP中的高级内容,看上去很复杂,但是非常有用,所以我做了长篇笔记.不然记不住,以后要用的时候,还是要从头学起. 由于这是供 ...

  3. PHP标准库 (SPL) 笔记

    简介 SPL是Standard PHP Library(PHP标准库)的缩写. The Standard PHP Library (SPL) is a collection of interfaces ...

  4. PHP 标准库 SPL 之数据结构栈(SplStack)简单实践

    PHP 5.3.0 版本及以上的堆栈描述可以使用标准库 SPL 中的 SplStack class,SplStack 类继承双链表 ( SplDoublyLinkedList ) 实现栈. 代码: & ...

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

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

  6. PHP标准库 SPL

    PHP SPL笔记 这几天,我在学习PHP语言中的SPL. 这个东西应该属于PHP中的高级内容,看上去很复杂,但是非常有用,所以我做了长篇笔记.不然记不住,以后要用的时候,还是要从头学起. 由于这是供 ...

  7. 【SPL标准库专题(1)】 SPL简介

    什么是SPL SPL是Standard PHP Library(PHP标准库)的缩写. 根据官方定义,它是"a collection of interfaces and classes th ...

  8. PHP标准库SPL

    SPL是Standard PHP Library(PHP标准库)的缩写.用来解决典型(常见)问题(common problems)的一组接口与类的集合 典型问题(common problems) - ...

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

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

  10. PHP SPL标准库之数据结构栈(SplStack)介绍(基础array已经可以解决很多问题了,现在开始解决问题)

    PHP SPL标准库之数据结构栈(SplStack)介绍(基础array已经可以解决很多问题了,现在开始解决问题) 一.总结 SplStack就是继承双链表(SplDoublyLinkedList)实 ...

随机推荐

  1. shell脚本sed awk

    删除第一行 sed '1d' test.txt 假装执行 sed -i '1d' test.txt 执行 从第二行删除到行尾 sed '2,$d' test.txt sed -i '2,$d' tes ...

  2. 【立即报名】解码AI大杀器:华为云GPU+Tensorflow 容器实战

    导语: 人工智能的火热,带来了一波学习TensorFlow深度学习框架的热潮.聊深度学习免不了要用GPU,但目前GPU费用较高,对于个人学习者和创业公司来讲的话,按需配置的云GPU服务器是一个不错的选 ...

  3. luogu P5514 [MtOI2019]永夜的报应

    题目背景 在这世上有一乡一林一竹亭,也有一主一仆一仇敌. 有人曾经想拍下他们的身影,却被可爱的兔子迷惑了心神. 那些迷途中的人啊,终究会消失在不灭的永夜中-- 题目描述 蓬莱山 辉夜(Kaguya)手 ...

  4. iOS 音频开发之CoreAudio

    转自:http://www.cnblogs.com/javawebsoa/archive/2013/05/20/3089511.html 接 触过IOS音频开发的同学都知道,Core Audio 是I ...

  5. ES6——async函数

    目录 1.async 函数是 Generator 函数的语法糖. 2.async函数对 Generator 函数的改进,体现在以下四点. 3.基本用法 一个获取股票报价的函数 指定多少毫秒后输出一个值 ...

  6. jQuery操作页面的元素

    主要有添加,替换,删除,清空三种方式: 添加又分为,在之前添加,在之后添加,在元素外之前添加,在元素外之后添加.每个添加方式又有俩种方法(效果一模一样): 俩种方法区添加,在原内容之后: p.appe ...

  7. HTML5学习第二天!

    学习了一天,然后整理内容到现在,感觉昨天的学习效率有点差,哎! 感受尽在代码中,布局真的脑壳疼,仅仅只整理了CSS中的list: <!DOCTYPE html> <html> ...

  8. openlayers5-webpack 入门开发系列结合 turf.js 实现等值线(附源码下载)

    前言 openlayers5-webpack 入门开发系列环境知识点了解: node 安装包下载webpack 打包管理工具需要依赖 node 环境,所以 node 安装包必须安装,上面链接是官网下载 ...

  9. 史上最全的iptables应用

    第14章 防火墙的使用 14.1 防火墙的概念 将不安全的网络流量信息进行隔离 14.2 防火墙的实现 14.2.1 硬件实现 思科,华为防火墙服务器 14.2.2 软件实现 iptables(cen ...

  10. LNMP-Nginx反向代理

    Nginx反向代理 Nginx提供反向代理的模块http proxy,这个模块是默认的,不需要重新编译模块.通常情况下,Nginx代理一般常用的环境是,提供web服务的服务器放在内网,暴露在外网上容易 ...