现在位置: 首页 > 博客文章 > 电脑相关 > IT开发 > 开发语言 > PHP > 正文
PHP中的迭代器用法
2015年06月11日 16:00:27 PHP ⁄ 共 2220字 暂无评论 ⁄ 被围观 2,752次

如果想在自定义类中重载迭代器,就需要执行一些 PHP 预定义的接口。

PHP中的迭代器用法

任何实现 Traversable 接口的类都可以用 foreach 结构遍历。但 Traversable 是一个空的接口而且不能被直接执行。可以执行 Iterator 或者 IteratorAggregate,它们都是从 Traversable 继承而来的。

Code   ViewPrint
  1. <?php  
  2.   
  3. class NumberSquared implements Iterator {  
  4.     private $start$end$cur;  
  5.   
  6.     public function __construct($start$end) {  
  7.         $this->start = $start;  
  8.         $this->end = $end;  
  9.     }  
  10.   
  11.     public function rewind() {  
  12.         $this->cur = $this->start;  
  13.     }  
  14.   
  15.     public function key() {  
  16.         return $this->cur;  
  17.     }  
  18.   
  19.     public function current() {  
  20.         return pow($this->cur, 2);  
  21.     }  
  22.   
  23.     public function next() {  
  24.         $this->cur++;  
  25.     }  
  26.   
  27.     public function valid() {  
  28.         return $this->cur <= $this->end;  
  29.     }  
  30. }  
  31.   
  32. $obj = new NumberSquared(4, 9);  
  33.   
  34. foreach ($obj as $key => $value) {  
  35.     print "The square of $key is $value<br />";  
  36. }  
  37.   
  38. ?>  

 

可以把类的执行和类的迭代器分离开来,改写上面的代码如下:

Code   ViewPrint
  1. <?php  
  2.   
  3. class NumberSquared implements IteratorAggregate {  
  4.     private $start$end;  
  5.   
  6.     public function __construct($start$end) {  
  7.         $this->start = $start;  
  8.         $this->end = $end;  
  9.     }  
  10.   
  11.     public function getIterator() {  
  12.         return new NumberSquaredIterator($this);  
  13.     }  
  14.   
  15.     public function getStart() {  
  16.         return $this->start;  
  17.     }  
  18.   
  19.     public function getEnd() {  
  20.         return $this->end;  
  21.     }  
  22. }  
  23.   
  24. class NumberSquaredIterator implements Iterator {  
  25.     private $obj$cur;  
  26.   
  27.     function __construct($obj) {  
  28.         $this->obj = $obj;  
  29.     }  
  30.   
  31.     public function rewind() {  
  32.         $this->cur = $this->obj->getStart();  
  33.     }  
  34.   
  35.     public function key() {  
  36.         return $this->cur;  
  37.     }  
  38.   
  39.     public function current() {  
  40.         return pow($this->cur, 2);  
  41.     }  
  42.   
  43.     public function next() {  
  44.         $this->cur++;  
  45.     }  
  46.   
  47.     public function valid() {  
  48.         return $this->cur <= $this->obj->getEnd();  
  49.     }  
  50. }  
  51.   
  52. $obj = new NumberSquared(4, 9);  
  53.   
  54. foreach ($obj as $key => $value) {  
  55.     print "The square of $key is $value<br />";  
  56. }  
  57.   
  58. ?>  

 

执行结果都是一样的,如下:

  1. The square of 4 is 16  
  2. The square of 5 is 25  
  3. The square of 6 is 36  
  4. The square of 7 is 49  
  5. The square of 8 is 64  
  6. The square of 9 is 81  

 

参考来源:《PHP权威编程》的第4章(PHP 5高级面向对象编程和设计模式)部分

给我留言

留言无头像?