激情久久久_欧美视频区_成人av免费_不卡视频一二三区_欧美精品在欧美一区二区少妇_欧美一区二区三区的

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - PHP教程 - PHP中迭代器的簡單實現(xiàn)及Yii框架中的迭代器實現(xiàn)方法示例

PHP中迭代器的簡單實現(xiàn)及Yii框架中的迭代器實現(xiàn)方法示例

2020-05-20 14:37胖胖的空間 PHP教程

這篇文章主要介紹了PHP中迭代器的簡單實現(xiàn)及Yii框架中的迭代器實現(xiàn)方法,結(jié)合實例形式分析了迭代器的原理及PHP與Yii框架中的迭代器的實現(xiàn)方法,需要的朋友可以參考下

本文實例講述了PHP中迭代器的簡單實現(xiàn)及Yii框架中的迭代器實現(xiàn)方法。分享給大家供大家參考,具體如下:

在維基百科中我們可以看到其定義如下:

迭代器有時又稱光標(cursor)是程式設計的軟件設計模式,可在容器物件(container,例如list或vector)上遍訪的接口,設計人員無需關心容器物件的內(nèi)容。

各種語言實作Iterator的方式皆不盡同,有些面向?qū)ο笳Z言像Java, C#, Python, Delphi都已將Iterator的特性內(nèi)建語言當中,完美的跟語言整合,我們稱之隱式迭代器(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的簡單實現(xiàn)】

?
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模式的簡單實現(xiàn)類
*/
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()方法的實現(xiàn)時有過糾結(jié),一直以為這里需要返回下一個的值,

這是因為一直以為這里的next就是next函數(shù)的實現(xiàn),但是非也

在手冊中我們可以看到其定義為

?
1
abstract public void Iterator::next ( void )

其返回值類型為void

所以這里我們調(diào)用next函數(shù)就可以了,沒有必要返回

另外,以上實現(xiàn)對于如下的數(shù)組是存在的問題

?
1
$data = array('0' => 11, "" => 22, 's3' => 33, 0, 0, "", false, 0, 1);

運行結(jié)果是輸出:

0 11
22
s3 33
1 0
2 0
3

false后面的值就沒有迭代顯示出來了,具體原因還不清楚,留作下回分解

在yii框架中也有實現(xiàn)迭代器,它的實現(xiàn)避免了這個問題。

【Yii框架中的迭代器實現(xiàn)】

在Yii框架中的我們可以看到其迭代器的實現(xiàn)

在collections目錄下的CMapIterator.php文件中,其實現(xiàn)如下:

?
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 />';
}

這與之前的簡單實現(xiàn)相比,其位置的變化是通過控制key來實現(xiàn)的,這種實現(xiàn)的作用是為了避免false作為數(shù)組值時無法迭代

希望本文所述對大家基于Yii框架的PHP程序設計有所幫助。

原文鏈接:http://www.phppan.com/2010/04/php-iterator-and-yii-cmapiterator/

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 成人精品一区二区三区中文字幕 | 羞羞视频免费网站含羞草 | 中国美女一级黄色片 | 国产美女的小嫩bbb图片 | h视频免费在线观看 | 精品国产一区二区三区久久久蜜 | 萌白酱福利视频在线网站 | 九九热久久免费视频 | 国产91亚洲精品久久久 | 激情在线视频 | 麻豆小视频在线观看 | 国产妞干网 | 国产精品久久av | 海外中文字幕在线观看 | 久久久国产精品免费观看 | 国产成年免费视频 | 中文字幕在线一 | 西川av在线一区二区三区 | 香蕉在线看 | 色视频在线观看 | 好吊色欧美一区二区三区四区 | 97视频| 国产成人精品区 | 天天操天天骑 | 成人午夜视频在线观看免费 | 国产一区日韩精品 | 国产一区视频在线观看免费 | 国产一级一国产一级毛片 | 黄色免费不卡视频 | 亚洲一级网站 | 欧美一级一区二区三区 | 亚洲成人欧美在线 | 久久久精品福利 | 欧美亚洲一区二区三区四区 | 在线天堂中文字幕 | 中文字幕精品一二三四五六七八 | 色污视频| 国产一区二区三区在线免费观看 | 久久人人爽人人爽人人片av高请 | 草久在线观看视频 | 免费观看的毛片手机视频 |