您的位置:首页 > PHP > PHP中迭代器的简单实现及Yii框架中的迭代器实现

PHP中迭代器的简单实现及Yii框架中的迭代器实现

PHP中迭代器的简单实现及Yii框架中的迭代器实现
在维基百科中我们可以看到其定义如下:
迭代器有时又称光标(cursor)是程式设计的软件设计模式,可在容器物件(container,例如list或vector)上遍访的接口,设计人员无需关心容器物件的内容。
各种语言实作Iterator的方式皆不尽同,有些面向对象语言像Java, C#, Python, Delphi都已将Iterator的特性内建语言当中,完美的跟语言整合,我们称之隐式迭代器(implicit iterator),但像是C++语言本身就没有Iterator的特色,但STL仍利用template实作了功能强大的iterator。
Iterator另一方面还可以整合Generator。有些语言将二者视为同一接口,有些语言则将之独立化。
地址:http://zh.wikipedia.org/zh-cn/%E8%BF%AD%E4%BB%A3%E5%99%A8
【Iterator的简单实现】

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
/**
* Iterator模式的简单实现类
*/
class sample implements Iterator {
    private $_items ;
 
    public function __construct(&$data) {
        $this->_items = $data;
    }
    public function current() {
        return current($this->_items);
    }
 
    public function next() {
        next($this->_items);   
    }
 
    public function key() {
        return key($this->_items);
    }
 
    public function rewind() {
        reset($this->_items);
    }
 
    public function valid() {                                                                              
        return ($this->current() !== FALSE);
    }
}
 
/** DEMO */
$data = array(1, 2, 3, 4, 5);
$sa = new sample($data);
foreach ($sa AS $key => $row) {
    echo $key, ' ', $row, '<br />';
}

在next()方法的实现时有过纠结,一直以为这里需要返回下一个的值,
这是因为一直以为这里的next就是next函数的实现,但是非也
在手册中我们可以看到其定义为
abstract public void Iterator::next ( void )
其返回值类型为void
所以这里我们调用next函数就可以了,没有必要返回
另外,以上实现对于如下的数组是存在的问题
$data = array(’0′ => 11, ” => 22, ’s3′ => 33, 0, 0, ”, false, 0, 1);
运行结果是输出:
0 11
22
s3 33
1 0
2 0
3
false后面的值就没有迭代显示出来了,具体原因还不清楚,留作下回分解
在yii框架中也有实现迭代器,它的实现避免了这个问题。

【Yii框架中的迭代器实现】
在Yii框架中的我们可以看到其迭代器的实现
在collections目录下的CMapIterator.php文件中,其实现如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
class CMapIterator implements Iterator {
/**
* @var array the data to be iterated through
*/
    private $_d;
/**
* @var array list of keys in the map
*/
    private $_keys;
/**
* @var mixed current key
*/
    private $_key;
 
/**
* Constructor.
* @param array the data to be iterated through
*/
    public function __construct(&$data) {
        $this->_d=&$data;
        $this->_keys=array_keys($data);
    }
 
/**
* Rewinds internal array pointer.
* This method is required by the interface Iterator.
*/
    public function rewind() {                                                                                 
        $this->_key=reset($this->_keys);
    }
 
/**
* Returns the key of the current array element.
* This method is required by the interface Iterator.
* @return mixed the key of the current array element
*/
    public function key() {
        return $this->_key;
    }
 
/**
* Returns the current array element.
* This method is required by the interface Iterator.
* @return mixed the current array element
*/
    public function current() {
        return $this->_d[$this->_key];
    }
 
/**
* Moves the internal pointer to the next array element.
* This method is required by the interface Iterator.
*/
    public function next() {
        $this->_key=next($this->_keys);
    }
 
/**
* Returns whether there is an element at current position.
* This method is required by the interface Iterator.
* @return boolean
*/
    public function valid() {
        return $this->_key!==false;
    }
}
 
$data = array('s1' => 11, 's2' => 22, 's3' => 33);
$it = new CMapIterator($data);
foreach ($it as $row) {
    echo $row, '<br />';
}

这与之前的简单实现相比,其位置的变化是通过控制key来实现的,这种实现的作用是为了避免false作为数组值时无法迭代

本文地址:PHP中迭代器的简单实现及Yii框架中的迭代器实现    文章出处:PHP源码阅读,PHP设计模式,PHP学习笔记,项目管理-胖胖的空间

转载请以链接形式注明原始出处和作者,谢绝不尊重版权者抄袭!

3 条留言我要留言

  •   1 - zzm  |  2011-09-05 at 2:03 pm  

    yii 这样的话 虽然可以避免false作为数组值的问题,但是还没能避免false作为键值的情况。

Trackbacks

  1. PHP源码阅读笔记二十四 :iterator实现中当值为false时无法完成迭代的原因分析 | 胖子的空间
  2. 思考PHP语言一:控制结构 | PHP源码阅读,PHP设计模式,PHP学习笔记-胖子的空间

必填

必填,绝不公开

*
To prove you're a person (not a spam script), type the security word shown in the picture. Click on the picture to hear an audio file of the word.
Click to hear an audio file of the anti-spam word