您好,欢迎访问一九零五行业门户网

PHP的标准库

这篇文章主要介绍了关于php的标准库 ,有着一定的参考价值,现在分享给大家,有需要的朋友可以参考一下
转:https://www.cnblogs.com/yafang/p/5872187.html 
一.什么是spl?
spl是用于解决典型问题(standard problems)的一组接口与类的集合。(出自:http://php.net/manual/zh/intro.spl.php)
spl,php 标准库(standard php library) ,从 php 5.0 起内置的组件和接口,且从 php5.3 已逐渐的成熟。spl 在所有的 php5 开发环境中被内置,同时无需任何设置。
二.如何使用?
spl提供了一组标准数据结构:
双向链表
splstack
splqueue
spldoublylinkedlist
双链表是一种重要的线性存储结构,对于双链表中的每个节点,不仅仅存储自己的信息,还要保存前驱和后继节点的地址。
php spl中的spldoublylinkedlist类提供了对双链表的操作。
spldoublylinkedlist类摘要如下:
1 spldoublylinkedlist implements iterator  , arrayaccess  , countable  {   2      3   public __construct ( void )   4   public void add ( mixed $index , mixed $newval )   5   //双链表的头部节点   6   public mixed top ( void )   7   //双链表的尾部节点   8   public mixed bottom ( void )   9   //双联表元素的个数  10   public int count ( void )  11   //检测双链表是否为空  12   public bool isempty ( void )  13     14     15   //当前节点索引  16   public mixed key ( void )  17   //移到上条记录  18   public void prev ( void )  19   //移到下条记录  20   public void next ( void )  21   //当前记录  22   public mixed current ( void )  23   //将指针指向迭代开始处  24   public void rewind ( void )  25   //检查双链表是否还有节点  26   public bool valid ( void )  27     28   //指定index处节点是否存在  29   public bool offsetexists ( mixed $index )  30   //获取指定index处节点值  31   public mixed offsetget ( mixed $index )  32   //设置指定index处值  33   public void offsetset ( mixed $index , mixed $newval )  34   //删除指定index处节点  35   public void offsetunset ( mixed $index )  36     37   //从双链表的尾部弹出元素  38   public mixed pop ( void )  39   //添加元素到双链表的尾部  40   public void push ( mixed $value )  41     42   //序列化存储  43   public string serialize ( void )  44   //反序列化  45   public void unserialize ( string $serialized )  46     47   //设置迭代模式  48   public void setiteratormode ( int $mode )  49   //获取迭代模式spldoublylinkedlist::it_mode_lifo (stack style) spldoublylinkedlist::it_mode_fifo (queue style)  50   public int getiteratormode ( void )  51     52   //双链表的头部移除元素  53   public mixed shift ( void )  54   //双链表的头部添加元素  55   public void unshift ( mixed $value )  56     57 }
使用起来也比较简单
1 $list = new spldoublylinkedlist();  2 $list->push('a');  3 $list->push('b');  4 $list->push('c'); 5 $list->push('d');  6   7 $list->unshift('top');  8 $list->shift();  9  10 $list->rewind();//rewind操作用于把节点指针指向bottom所在的节点 11 echo 'curren node:'.$list->current().<br />;//获取当前节点 12  13 $list->next();//指针指向下一个节点 14 echo 'next node:'.$list->current().<br />; 15  16 $list->next(); 17 $list->next(); 18 $list->prev();//指针指向上一个节点 19 echo 'next node:'.$list->current().<br />; 20  21 if($list->current()) 22     echo 'current node is valid<br />'; 23 else 24     echo 'current node is invalid<br />'; 25  26  27 if($list->valid())//如果当前节点是有效节点,valid返回true 28     echo valid list<br />; 29 else 30     echo invalid list <br />; 31  32  33 var_dump(array( 34     'pop' => $list->pop(), 35     'count' => $list->count(), 36     'isempty' => $list->isempty(), 37     'bottom' => $list->bottom(), 38     'top' => $list->top() 39 ));  40  41 $list->setiteratormode(spldoublylinkedlist::it_mode_fifo); 42 var_dump($list->getiteratormode()); 43  44 for($list->rewind(); $list->valid(); $list->next()) { 45     echo $list->current().php_eol; 46 } 47  48 var_dump($a = $list->serialize()); 49 //print_r($list->unserialize($a)); 50  51 $list->offsetset(0,'new one'); 52 $list->offsetunset(0); 53 var_dump(array( 54     'offsetexists' => $list->offsetexists(4), 55     'offsetget' => $list->offsetget(0), 56  57 )); 58 var_dump($list); 59  60 //堆栈,先进后出 61 $stack = new splstack();//继承自spldoublylinkedlist类 62  63 $stack->push(a<br />); 64 $stack->push(b<br />); 65  66 echo $stack->pop(); 67 echo $stack->pop(); 68 echo $stack->offsetset(0,'b');//堆栈的offset=0是top所在的位置,offset=1是top位置节点靠近bottom位置的相邻节点,以此类推 69 $stack->rewind();//双向链表的rewind和堆栈的rewind相反,堆栈的rewind使得当前指针指向top所在的位置,而双向链表调用之后指向bottom所在位置70 echo 'current:'.$stack->current().'<br />'; 71  72 $stack->next();//堆栈的next操作使指针指向靠近bottom位置的下一个节点,而双向链表是靠近top的下一个节点 73 echo 'current:'.$stack->current().'<br />'; 74 echo '<br /><br />'; 75  76 //队列,先进先出 77 $queue = new splqueue();//继承自spldoublylinkedlist类 78  79 $queue->enqueue(a<br />);//插入一个节点到队列里面的top位置 80 $queue->enqueue(b<br />); 81  82 $queue->offsetset(0,'a');//堆栈的offset=0是top所在的位置,offset=1是top位置节点靠近bottom位置的相邻节点,以此类推 83  84 echo $queue->dequeue(); 85 echo $queue->dequeue(); 86  87 echo <br /><br />;

splmaxheap
splminheap
splheap
堆(heap)就是为了实现优先队列而设计的一种数据结构,它是通过构造二叉堆(二叉树的一种)实现。根节点最大的堆叫做最大堆或大根堆(splmaxheap),根节点最小的堆叫做最小堆或小根堆(splminheap)。二叉堆还常用于排序(堆排序)。
splheap类摘要如下:
1 abstract splheap implements iterator , countable { 2 /* 方法 */  3 public __construct ( void )  4 abstract protected int compare ( mixed $value1 , mixed $value2 )  5 public int count ( void )  6 public mixed current ( void )  7 public mixed extract ( void )  8 public void insert ( mixed $value )  9 public bool isempty ( void ) 10 public mixed key ( void ) 11 public void next ( void ) 12 public void recoverfromcorruption ( void ) 13 public void rewind ( void ) 14 public mixed top ( void ) 15 public bool valid ( void ) 16 }
显然它是一个抽象类,最大堆(splmaxheap)和最小堆(splminheap)就是继承它实现的。最大堆和最小堆并没有额外的方法。
splheap简单使用:
1 //堆  2 class mysplheap extends splheap{  3     //compare()方法用来比较两个元素的大小,绝对他们在堆中的位置  4     public function compare( $value1, $value2 ) {  5         return ( $value1 - $value2 );  6     }  7 }  8   9 $obj = new mysplheap(); 10  11 $obj->insert(0); 12 $obj->insert(1); 13 $obj->insert(2); 14 $obj->insert(3); 15 $obj->insert(4); 16  17 echo $obj->top();//4 18 echo $obj->count();//5 19  20 foreach ($obj as $item) { 21     echo $item.<br />; 22 }
队列
splpriorityqueue
优先队列也是非常实用的一种数据结构,可以通过加权对值进行排序,由于排序在php内部实现,业务代码中将精简不少而且更高效。通过splpriorityqueue::setextractflags(int  $flag)设置提取方式可以提取数据(等同最大堆)、优先级、和两者都提取的方式。
splpriorityqueue类摘要如下:
1 splpriorityqueue implements iterator , countable {  2 /* 方法 */  3 public __construct ( void )  4 public int compare ( mixed $priority1 , mixed $priority2 )  5 public int count ( void )  6 public mixed current ( void )  7 public mixed extract ( void )  8 public void insert ( mixed $value , mixed $priority )  9 public bool isempty ( void ) 10 public mixed key ( void ) 11 public void next ( void ) 12 public void recoverfromcorruption ( void ) 13 public void rewind ( void ) 14 public void setextractflags ( int $flags ) 15 public mixed top ( void ) 16 public bool valid ( void ) 17 }
简单使用:
1 $pq = new splpriorityqueue();  2   3 $pq->insert('a', 10);  4 $pq->insert('b', 1);  5 $pq->insert('c', 8);  6   7 echo $pq->count() .php_eol; //3  8 echo $pq->current() . php_eol; //a  9  10 /** 11  * 设置元素出队模式 12  * splpriorityqueue::extr_data 仅提取值 13  * splpriorityqueue::extr_priority 仅提取优先级 14  * splpriorityqueue::extr_both 提取数组包含值和优先级 15  */ 16 $pq->setextractflags(splpriorityqueue::extr_data); 17  18 while($pq->valid()) { 19     print_r($pq->current());  //a  c  b 20     $pq->next(); 21 }
阵列
splfixedarray
splfixedarray主要是处理数组相关的主要功能,与普通php array不同的是,它是固定长度的,且以数字为键名的数组,优势就是比普通的数组处理更快。通常情况下splfixedarray要比php array快上20%~30%,所以如果你是处理巨大数量的固定长度数组,还是强烈建议使用。
splfixedarray类摘要如下:
1 splfixedarray implements iterator , arrayaccess , countable { 2 /* 方法 */  3 public __construct ([ int $size = 0 ] )  4 public int count ( void )  5 public mixed current ( void )  6 public static splfixedarray fromarray ( array $array [, bool $save_indexes = true ] )  7 public int getsize ( void )  8 public int key ( void )  9 public void next ( void ) 10 public bool offsetexists ( int $index ) 11 public mixed offsetget ( int $index ) 12 public void offsetset ( int $index , mixed $newval ) 13 public void offsetunset ( int $index ) 14 public void rewind ( void ) 15 public int setsize ( int $size ) 16 public array toarray ( void ) 17 public bool valid ( void ) 18 public void __wakeup ( void ) 19 }
简单使用:
1 $arr = new splfixedarray(4); 2 $arr[0] = 'php';  3 $arr[1] = 1;  4 $arr[3] = 'python';  5   6 //遍历, $arr[2] 为null  7 foreach($arr as $v) {  8     echo $v . php_eol;  9 } 10  11 //获取数组长度 12 echo $arr->getsize(); //4 13  14 //增加数组长度 15 $arr->setsize(5); 16 $arr[4] = 'new one'; 17  18 //捕获异常 19 try{ 20     echo $arr[10]; 21 } catch (runtimeexception $e) { 22     echo $e->getmessage(); 23 }
映射
splobjectstorage
用来存储一组对象的,特别是当你需要唯一标识对象的时候。
php spl splobjectstorage类实现了countable,iterator,serializable,arrayaccess四个接口。可实现统计、迭代、序列化、数组式访问等功能。
splobjectstorage类摘要如下:
1 splobjectstorage implements countable , iterator , serializable , arrayaccess {  2 /* 方法 */  3 public void addall ( splobjectstorage $storage )  4 public void attach ( object $object [, mixed $data = null ] )  5 public bool contains ( object $object )  6 public int count ( void )  7 public object current ( void )  8 public void detach ( object $object )  9 public string gethash ( object $object ) 10 public mixed getinfo ( void ) 11 public int key ( void ) 12 public void next ( void ) 13 public bool offsetexists ( object $object ) 14 public mixed offsetget ( object $object ) 15 public void offsetset ( object $object [, mixed $data = null ] ) 16 public void offsetunset ( object $object ) 17 public void removeall ( splobjectstorage $storage ) 18 public void removeallexcept ( splobjectstorage $storage ) 19 public void rewind ( void ) 20 public string serialize ( void ) 21 public void setinfo ( mixed $data ) 22 public void unserialize ( string $serialized ) 23 public bool valid ( void ) 24 }
简单使用:
1 class a {  2     public $i;  3     public function __construct($i) {  4         $this->i = $i;  5     }  6 }  7    8 $a1 = new a(1);  9 $a2 = new a(2); 10 $a3 = new a(3); 11 $a4 = new a(4); 12   13 $container = new splobjectstorage(); 14   15 //splobjectstorage::attach 添加对象到storage中 16 $container->attach($a1); 17 $container->attach($a2); 18 $container->attach($a3); 19   20 //splobjectstorage::detach 将对象从storage中移除 21 $container->detach($a2); 22   23 //splobjectstorage::contains用于检查对象是否存在storage中 24 var_dump($container->contains($a1)); //true 25 var_dump($container->contains($a4)); //false 26   27 //遍历 28 $container->rewind(); 29 while($container->valid()) { 30     var_dump($container->current()); 31     $container->next(); 32 }
以上就是php的标准库 的详细内容。
其它类似信息

推荐信息