Php SPL库中的迭代器接口详解

本文详细介绍了PHP标准库(SPL)中的六种迭代器接口,包括Traversable、Iterator、IteratorAggregate、OuterIterator、RecursiveIterator及SeekableIterator,并通过具体示例展示了它们的使用方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

         Php SPL库中的迭代器接口详解


SPL库中的迭代器接口

SPL提供了6个迭代器接口,如下表

Traversable遍历接口(检测一个类是否可以使用 foreach 进行遍历的接口)
Iterator迭代器接口(可在内部迭代自己的外部迭代器或类的接口)
IteratorAggregate聚合式迭代器接口(创建外部迭代器的接口)
OuterIterator迭代器嵌套接口(将一个或多个迭代器包裹在另一个迭代器中)
RecursiveIterator递归迭代访问接口(提供递归访问功能)
SeekableIterator可索引迭代访问接口(实现查找功能)

1、Traversable接口

Traversable接口实际上不是一个接口,在实际写php代码中不能用。因为只有内部的PHP类(用C写的类)才可以直接实现Traversable接口。可以说这是个特性级别的东西。实际的PHP编程中我们使用Iterator接口或者IteratorAggregate接口来实现遍历。

2、Iterator接口

 Iterator接口的主要用途是允许一个类实现一个基本的迭代功能,从而使它可以被循环访问,根据键值访问以及回滚。

【Iterator接口摘要】

  1. Iterator extends Traversable  
  2. {  
  3.     //返回当前索引游标指向的元素  
  4.     abstract public mixed current(void)  
  5.     //返回当前索引游标指向的元素的键名  
  6.     abstract public scalar key(void)  
  7.     //移动当前索引游标指向下一元素  
  8.     abstract public void next(void)  
  9.     //重置索引游标的指向第一个元素  
  10.     abstract public void rewind(void)  
  11.     //判断当前索引游标指向的是否是一个元素,常常在调用 rewind()或 next()使用  
  12.     abstract public boolean valid(void)  
  13. }  

【Iterator使用实例】

<代码>

  1. class myIterator implements Iterator  
  2. {  
  3.     //索引游标  
  4.     private $position = 0;  
  5.     private $array = array(  
  6.         "第1个元素",  
  7.         "第2个元素",  
  8.         "最后1个元素",  
  9.     );  
  10.   
  11.     public function __construct()  
  12.     {  
  13.         $this->position = 0;  
  14.     }  
  15.   
  16.     function rewind()  
  17.     {  
  18.         echo "调用rewind方法\n";  
  19.         $this->position = 0;  
  20.     }  
  21.   
  22.     function current()  
  23.     {  
  24.         echo "调用current方法\n";  
  25.         return $this->array[$this->position];  
  26.     }  
  27.   
  28.     function key()  
  29.     {  
  30.         echo "调用key方法\n";  
  31.         return $this->position;  
  32.     }  
  33.   
  34.     function next()  
  35.     {  
  36.         echo "调用next方法\n";  
  37.         ++$this->position;  
  38.     }  
  39.   
  40.     function valid()  
  41.     {  
  42.         echo "调用valid方法\n";  
  43.         return isset($this->array[$this->position]);  
  44.     }  
  45. }  
  46.   
  47. $it = new myIterator;  
  48. $times = 0;  
  49. foreach ($it as $key => $value) {  
  50.     $times ++;  
  51.     echo "键名:{$key}  值:{$value}\n";  
  52.     echo "----------第{$times}遍历完成--------->\n";  
  53. }  

<输出>


3、IteratorAggregate接口

创建外部迭代器的接口,在文章《Php设计模式之迭代器模式Iterator Pattern(一) 》中介绍的迭代器就是这种。

【IteratorAggregate接口摘要】

  1. IteratorAggregate extends Traversable {  
  2. //实现该方法时,必须返回一个实现了Iterator接口的类的实例  
  3. abstract public Traversable getIterator ( void )  
  4. }  

SPL还提供了一些专门用来与IteratorAggregate接口一起使用的内置迭代器。使用这些迭代器意味着只需要实现一个方法并实例化一个类就可以使对象可以迭代访问了。

【Iterator使用实例】使用IteratorAggregate接口与ArrayIterator迭代器类输出类中公共属性

<代码>

  1. class myData implements IteratorAggregate  
  2. {  
  3.     public $property1 = "公共属性1";  
  4.     public $property2 = "公共属性2";  
  5.     public $property3 = "公共属性3";  
  6.   
  7.     public function __construct()  
  8.     {  
  9.         $this->property4 = "最后一个公共属性";  
  10.     }  
  11.   
  12.     public function getIterator()  
  13.     {  
  14.         return new ArrayIterator($this);  
  15.     }  
  16. }  
  17.   
  18. $obj = new myData;  
  19. foreach ($obj as $key => $value) {  
  20.     echo "键名:{$key}  值:{$value}\n";  
  21. }  

<输出>


4、OuterIterator接口

迭代器嵌套接口(将一个或多个迭代器包裹在另一个迭代器中)

【OuterIterator接口摘要】

  1. OuterIterator extends Iterator {  
  2. /* 方法 */  
  3. public Iterator getInnerIterator ( void )  
  4. /* 继承Iterator 的方法 */  
  5. abstract public mixed Iterator::current ( void )  
  6. abstract public scalar Iterator::key ( void )  
  7. abstract public void Iterator::next ( void )  
  8. abstract public void Iterator::rewind ( void )  
  9. abstract public boolean Iterator::valid ( void )  
  10. }  

这个接口与IteratorAggregate接口的区别在于增加了 getInnerIterator方法。getInnerIterator方法返回当前正在迭代访问的迭代器。

【OuterIterator的使用】

这一接口与其它几个更加专用的迭代器的基础接口,其中包括AppendIterator、CachingIterator、FilterIterator、teratorIterator、LimitIterator、RecursiveIteratorIterator。

5、RecursiveIterator接口

RecursiveIterator接口提供了递归迭代访问功能,这种接口可以描述一个树形的数据结构,其中包含了节点\叶子或者父\子元素。它继承了Iterator界面,因而也具有标准的current()、key()、next()、 rewind()和valid()方法。同时,它自己还规定了getChildren()和hasChildren()方法。

【RecursiveIterator接口概要】

  1. RecursiveIterator extends Iterator {  
  2. /* 方法 */  
  3. public RecursiveIterator getChildren ( void )  
  4. public bool hasChildren ( void )  
  5. /* 继承的方法 */  
  6. abstract public mixed Iterator::current ( void )  
  7. abstract public scalar Iterator::key ( void )  
  8. abstract public void Iterator::next ( void )  
  9. abstract public void Iterator::rewind ( void )  
  10. abstract public boolean Iterator::valid ( void )  
  11. }  

【RecursiveIterator接口示例】递归遍历多维数组

<代码>

  1. class MyRecursiveIterator implements RecursiveIterator  
  2. {  
  3.     private $_data;  
  4.     private $_position = 0;  
  5.   
  6.     public function __construct(array $data)  
  7.     {  
  8.         $this->_data = $data;  
  9.     }  
  10.   
  11.     public function valid()  
  12.     {  
  13.         return isset($this->_data[$this->_position]);  
  14.     }  
  15.   
  16.     public function hasChildren()  
  17.     {  
  18.         return is_array($this->_data[$this->_position]);  
  19.     }  
  20.   
  21.     public function next()  
  22.     {  
  23.         $this->_position++;  
  24.     }  
  25.   
  26.     public function current()  
  27.     {  
  28.         return $this->_data[$this->_position];  
  29.     }  
  30.   
  31.     public function getChildren()  
  32.     {  
  33.         print_r($this->_data[$this->_position]);  
  34.     }  
  35.   
  36.     public function rewind()  
  37.     {  
  38.         $this->_position = 0;  
  39.     }  
  40.   
  41.     public function key()  
  42.     {  
  43.         return $this->_position;  
  44.     }  
  45. }  
  46.   
  47. $arr = array(  
  48.     0, 1, 2, 3, 4,  
  49.     5 => array(10, 20, 30), 6, 7, 8,  
  50.     9 => array(1, 2, 3)  
  51. );  
  52. $mri = new MyRecursiveIterator($arr);  
  53. foreach ($mri as $c => $v) {  
  54.     if ($mri->hasChildren()) {  
  55.         echo "第{$c}个元素含有子元素: \n";  
  56.         $mri->getChildren();  
  57.     } else {  
  58.         echo "第{$c}个元素:$v \n";  
  59.     }  
  60. }  


<输出>


6、 SeekableIterator接口

SeekableIterator接口也是Iterator接口的延伸,除了Iterator的5个方法以外,还规定了seek()方法,参数是元素的位置,返回该元素。如果该位置不存在,则抛出OutOfBoundsException。

【SeekableIterator接口概要】

  1. SeekableIterator extends Iterator {  
  2. /* 方法 */  
  3. abstract public void seek ( int $position )  
  4. /* 继承的方法 */  
  5. abstract public mixed Iterator::current ( void )  
  6. abstract public scalar Iterator::key ( void )  
  7. abstract public void Iterator::next ( void )  
  8. abstract public void Iterator::rewind ( void )  
  9. abstract public boolean Iterator::valid ( void )  
  10. }  

【SeekableIterator接口示例】如何创建的的定制的SeekableIterator接口,并处理无效的游标异常。

<代码>

  1. class MySeekableIterator implements SeekableIterator  
  2. {  
  3.   
  4.     private $position = 0;  
  5.     private $array = array(  
  6.         "元素1",  
  7.         "元素2",  
  8.         "元素3",  
  9.         "元素4"  
  10.     );  
  11.     /** 
  12.      * Seek方法实现 
  13.      * @param $position 
  14.      * @throws OutOfBoundsException 
  15.      */  
  16.     public function seek($position)  
  17.     {  
  18.         $this->position = $position;  
  19.         if (!$this->valid()) {  
  20.             throw new OutOfBoundsException("invalid seek position ($position)");  
  21.         }  
  22.     }  
  23.   
  24.     /* Iterator接口中实现的方法 */  
  25.     public function __construct()  
  26.     {  
  27.         $this->position = 0;  
  28.     }  
  29.   
  30.     public function rewind()  
  31.     {  
  32.         $this->position = 0;  
  33.     }  
  34.   
  35.     public function current()  
  36.     {  
  37.         return $this->array[$this->position];  
  38.     }  
  39.   
  40.     public function key()  
  41.     {  
  42.         return $this->position;  
  43.     }  
  44.   
  45.     public function next()  
  46.     {  
  47.         ++$this->position;  
  48.     }  
  49.   
  50.     public function valid()  
  51.     {  
  52.         return isset($this->array[$this->position]);  
  53.     }  
  54. }  
  55.   
  56. try {  
  57.     $it = new MySeekableIterator;  
  58.     echo $it->current(), "\n";  
  59.     $it->seek(2);  
  60.     echo $it->current(), "\n";  
  61.     $it->seek(1);  
  62.     echo $it->current(), "\n";  
  63.     $it->seek(10);  
  64. } catch (OutOfBoundsException $e) {  
  65.     echo "无效游标位置:".$e->getMessage();  
  66. }  

<输出>

php标准库(SPL库)中的迭代器接口目前至php5.3.13版本就提供了这些。

转:http://blog.youkuaiyun.com/uuleaf/article/details/7591291

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值