首页 > 作文

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

更新时间:2023-04-07 23:41:09 阅读: 评论:0

一、什么是spl库?

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

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

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

二、spl如何使用?

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

更详细的情况可参考

双向链表

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

spldoublylinkedlist

splstack(栈)

splqueue(队列)

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 impty ( void )//检测双链表是否为空    public mixed key ( void )//当前节点索引    public void next ( void )//移到下条记录    public bool offtexists ( mixed $index )//指定index处节点是否存在    public mixed offtget ( mixed $index )//获取指定index处节点值    public void offtt ( mixed $index , mixed $newval )//设置指定index处值    public void offtunt ( mixed $index )//删除指定index处节点    public mixed pop ( void )//从双链表的尾部弹出元素    public void prev ( void )//移到上条记录    public void push ( mixed $value )//添加元素到双链表的尾部    public void rewind ( void )//将指针指向迭代开始处    public string rialize ( void )//序列化存储    public void titeratormode ( int $mode )//设置迭代模式    public mixed shift ( void )//双链表的头部移除元素    public mixed top ( void )//双链表的头部节点    public void unrialize ( string $rialized )//反序列化    public void unshift ( mixed $value )//双链表的头部添加元素    public bool valid ( void )//检查双链表是否还有节点}

接下来是使用方法:

$list=newspldoublylinkedlist();
$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 />';el    echo 'current node is invalid<br />';     if($list->valid())//如果当前节点是有效节点,valid返回true    echo "valid list<br />";el  echo "invalid list <br />"; var_dump(array(    'pop' => $list->pop(),    'count' => $list->count(),        'impty' => $list->impty(),        'bottom' => $list->bottom(),        'top' => $list->top())); $list->titeratormode(spldoublylinkedlist::it_mode_fifo);var_dump($list->getiteratormode()); for($list->rewind(); $list->valid(); $list->next()){    echo $list->current().php_eol;} var_dump($a = $list->rialize());//print_r($list->unrialize($a)); $list->offtt(0,'new one');$list->offtunt(0);var_dump(array(    'offtexists' => $list->offtexists(4),        'offtget' => $list->offtget(0),));var_dump($list); //堆栈,先进后出$stack = new splstack();//继承自spldoublylinkedlist类 $stack->push("a<br />");$stack->push("b<br />"); echo $stack->pop();echo $stack->pop();echo $stack->offtt(0,'b');//堆栈的offt=0是top所在的位置,offt=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->offtt(0,'a');//堆栈的of我们都是追梦人ft=0是top所在的位置,offt=1是top位置节点靠近bottom位置的相邻节点,以此类推echo $queue->dequeue();echo $queue->dequeue();echo "<br /><br />";

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

splheap

splmaxheap

splminheap

splpriorityqueue

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 inrt ( mixed $value )        public bool impty ( 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->inrt(0);$obj->inrt(1);$obj->inrt(2);$obj->inrt(3);$obj->inrt(4);echo $obj->top();//4echo $obj->count();//5 foreach ($obj as $item) {    echo $item."<br />";}

阵列

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

splfixedarray

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 offtexists ( int $index )  public mixed offtget ( int $index )  public void offtt ( int $index , mixed $newval )  public void offtunt ( int $index )  public void rewind ( void )  public int tsize ( 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] = 'pyt十月旅游hon';//遍历, $arr[2] 为nullforeach($arr as $v) {    echo $v . php_eol;} //获取数组长度echo $arr->getsize(); //4 //增加数组长度$arr->tsize(5);$arr[4] = 'new one'; //捕获异常try{    echo $arr[10];} catch (runtimeexception $e) {    echo $e->getmessage();}


映射

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

splobjectstorage

splobjectstorage implements countable , iterator , rializable , 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 offtexists ( object $object )  public mixed offtget ( object $object )  public void offtt ( object $object [, mixed $data = null ] )  public void offtunt ( object $object )  public void removeall ( splobjectstorage $storage )  public void removeallexcept ( splobjectstorage $storage )  public void rewind ( void )  public string rialize ( void )  public void tinfo ( mixed $d描写雨的诗句ata )  public void unrialize ( string $rialized )  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)); //truevar_dump($container->contains($a4)); //fal //遍历$container->rewind();while($container->valid()) {    var_dump($container->current());        $container->next();}

原文地址:

本文发布于:2023-04-07 23:41:07,感谢您对本站的认可!

本文链接:https://www.wtabcd.cn/fanwen/zuowen/df8eff5f1883bc573579f514bda6212a.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

本文word下载地址:深入浅出 PHP SPL(PHP 标准库)(转).doc

本文 PDF 下载地址:深入浅出 PHP SPL(PHP 标准库)(转).pdf

标签:节点   链表   堆栈   位置
相关文章
留言与评论(共有 0 条评论)
   
验证码:
Copyright ©2019-2022 Comsenz Inc.Powered by © 专利检索| 网站地图