简介:这是php设计模式 Interator (迭代器模式)的详细页面,介绍了和php,有关的知识、技巧、经验,和一些php源码等。
class='pingjiaF' frameborder='0' src='http://biancheng.dnbcw.info/pingjia.php?id=339659' scrolling='no'>1 <?php
2 /**
3 * 迭代器模式
4 *
5 * 提供一个方法顺序访问一聚合对象中的各个元素,而又不暴露对象的内部表示
6 */
7 interface Interator
8 {
9 public function next();
10 public function first();
11 public function current();
12 public function isDone();
13 }
14
15 class SomeInterator implements Interator
16 {
17 private $_arr = array();
18
19 public function __construct($arr)
20 {
21 $this->_arr = $arr;
22 }
23
24 public function first()
25 {
26 return $this->_arr[0];
27 }
28
29 public function current()
30 {
31 return current($this->_arr);
32 }
33
34 public function next()
35 {
36 return next($this->_arr);
37 }
38
39 public function isDone()
40 {
41 }
42 }
43
44 $objSomeInterator = new SomeInterator(array(1,2,3,4,6,7));
45 echo $objSomeInterator->first(),"<br/>";
46 echo $objSomeInterator->next(),"<br/>";
47 echo $objSomeInterator->current(),"<br/>";
48 echo $objSomeInterator->current(),"<br/>";
49 echo $objSomeInterator->next(),"<br/>";
50 echo $objSomeInterator->current(),"<br/>";