亚洲精品久久久中文字幕-亚洲精品久久片久久-亚洲精品久久青草-亚洲精品久久婷婷爱久久婷婷-亚洲精品久久午夜香蕉

您的位置:首頁技術文章
文章詳情頁

PHP中迭代器的簡單實現及Yii框架中的迭代器實現方法示例

瀏覽:106日期:2022-09-10 16:24:08

本文實例講述了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的簡單實現】

/*** 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 1122s3 331 02 03

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

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

【Yii框架中的迭代器實現】

在Yii框架中的我們可以看到其迭代器的實現

在collections目錄下的CMapIterator.php文件中,其實現如下:

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作為數組值時無法迭代

更多關于Yii相關內容感興趣的讀者可查看本站專題:《Yii框架入門及常用技巧總結》、《php優秀開發框架總結》、《smarty模板入門基礎教程》、《php面向對象程序設計入門教程》、《php字符串(string)用法總結》、《php+mysql數據庫操作入門教程》及《php常見數據庫操作技巧匯總》

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

標簽: PHP
相關文章:
主站蜘蛛池模板: 中文字幕中出在线 | 欧美人与鲁交大毛片免费 | 黄色片在线观看网站 | 98pao强力打造高清免费 | 91福利社在线观看 | 九九久久99综合一区二区 | 免费大片a一级一级 | 日本免费人成黄页网观看视频 | 国产日产欧产精品精品推荐在线 | 久草在线播放视频 | 伊人色婷婷综在合线亚洲 | 一级国产20岁美女毛片 | 亚洲香蕉国产高清在线播放 | 国产精品tv | 国产精品成人aaaaa网站 | 一区二区三区四区亚洲 | 激情在线观看视频免费的 | 久久伊人热 | 国产一区二区三区四区偷看 | 国产vr一区二区在线观看 | 国自产在线精品免费 | 精品国产香蕉在线播出 | 国产免费一区二区三区最新 | 一级一片在线播放在线观看 | 国产精品拍自在线观看 | 一区二区3区免费视频 | 亚洲综合一区二区不卡 | 一级α片 | 50岁老女人毛片一级亚洲 | 清纯唯美综合网 | 伊人院| 精品久久久日韩精品成人 | 国产日产精品_国产精品毛片 | 国产精品人伦久久 | 国产区亚洲区 | 亚洲欧美日韩综合在线一区二区三区 | a级黄色网址 | 亚洲一区二区三区夜色 | www.av在线.com | 午夜网站在线播放 | 久久国产自偷自偷免 |