国产成人精品久久免费动漫-国产成人精品天堂-国产成人精品区在线观看-国产成人精品日本-a级毛片无码免费真人-a级毛片毛片免费观看久潮喷

您的位置:首頁(yè)技術(shù)文章
文章詳情頁(yè)

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

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

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

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

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

各種語(yǔ)言實(shí)作Iterator的方式皆不盡同,有些面向?qū)ο笳Z(yǔ)言像Java, C#, Python, Delphi都已將Iterator的特性內(nèi)建語(yǔ)言當(dāng)中,完美的跟語(yǔ)言整合,我們稱之隱式迭代器(implicit iterator),但像是C++語(yǔ)言本身就沒(méi)有Iterator的特色,但STL仍利用template實(shí)作了功能強(qiáng)大的iterator。

Iterator另一方面還可以整合Generator。有些語(yǔ)言將二者視為同一接口,有些語(yǔ)言則將之獨(dú)立化。地址:http://zh.wikipedia.org/zh-cn/%E8%BF%AD%E4%BB%A3%E5%99%A8

【Iterator的簡(jiǎn)單實(shí)現(xiàn)】

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

這是因?yàn)橐恢币詾檫@里的next就是next函數(shù)的實(shí)現(xiàn),但是非也

在手冊(cè)中我們可以看到其定義為

abstract public void Iterator::next ( void )

其返回值類型為void

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

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

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

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

0 1122s3 331 02 03

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

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

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

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

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

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 />’;}

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

更多關(guān)于Yii相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Yii框架入門及常用技巧總結(jié)》、《php優(yōu)秀開(kāi)發(fā)框架總結(jié)》、《smarty模板入門基礎(chǔ)教程》、《php面向?qū)ο蟪绦蛟O(shè)計(jì)入門教程》、《php字符串(string)用法總結(jié)》、《php+mysql數(shù)據(jù)庫(kù)操作入門教程》及《php常見(jiàn)數(shù)據(jù)庫(kù)操作技巧匯總》

希望本文所述對(duì)大家基于Yii框架的PHP程序設(shè)計(jì)有所幫助。

標(biāo)簽: PHP
相關(guān)文章:
主站蜘蛛池模板: 国产黄三级三·级三级 | 精品国产高清久久久久久小说 | 亚洲国产一区二区a毛片 | 亚洲欧美日韩精品久久久 | 国产免费久久精品 | 国产精品1区 2区 3区 | 亚洲男同视频网站 | 国产欧美综合在线一区二区三区 | 成人免费观看永久24小时 | 日本一二线不卡在线观看 | 精品无码一区在线观看 | 国产一区二区三区日韩欧美 | 九九在线偷拍视频在线播放 | 亚洲激情欧美 | 免费在线观看a级毛片 | 欧美午夜视频一区二区三区 | 亚洲一级大片 | 国产综合精品一区二区 | 国产一级毛片国语版 | 日韩在线成人 | 国产精品美女一区二区 | 99精品在免费线视频 | 美国亚洲成年毛片 | 94欧美setu | 国产一及毛片 | 伊人青 | 亚洲国产剧情在线精品视 | 久久女同互慰一区二区三区 | 狠狠色狠狠色综合日日32 | 中文字幕在线观看91 | 国产精品影视 | 欧美亚洲国产日韩一区二区三区 | 欧美无极品 | 最新中文字幕乱码在线 | 美女午夜影院 | 怡红院视频网 | 黄色成人免费网站 | 99精品免费观看 | 九九热视频在线免费观看 | 国产乱理片在线观看夜 | 欧美一区二区三区在线视频 |