PHP中迭代器的簡(jiǎn)單實(shí)現(xiàn)及Yii框架中的迭代器實(shí)現(xiàn)方法示例
本文實(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ǔ)言本身就沒有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ù)就可以了,沒有必要返回
另外,以上實(shí)現(xiàn)對(duì)于如下的數(shù)組是存在的問(wèn)題
$data = array('0' => 11, "" => 22, 's3' => 33, 0, 0, "", false, 0, 1);
運(yùn)行結(jié)果是輸出:
0 11
22
s3 33
1 0
2 0
3
false后面的值就沒有迭代顯示出來(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)秀開發(fā)框架總結(jié)》、《smarty模板入門基礎(chǔ)教程》、《php面向?qū)ο蟪绦蛟O(shè)計(jì)入門教程》、《php字符串(string)用法總結(jié)》、《php+mysql數(shù)據(jù)庫(kù)操作入門教程》及《php常見數(shù)據(jù)庫(kù)操作技巧匯總》
希望本文所述對(duì)大家基于Yii框架的PHP程序設(shè)計(jì)有所幫助。
- php框架CI(codeigniter)自動(dòng)加載與自主創(chuàng)建對(duì)象操作實(shí)例分析
- thinkphp 框架數(shù)據(jù)庫(kù)切換實(shí)現(xiàn)方法分析
- Thinkphp 框架配置操作之配置加載與讀取配置實(shí)例分析
- Thinkphp 框架基礎(chǔ)之入口文件功能、定義與用法分析
- Thinkphp 框架基礎(chǔ)之源碼獲取、環(huán)境要求與目錄結(jié)構(gòu)分析
- Thinkphp 框架擴(kuò)展之驅(qū)動(dòng)擴(kuò)展實(shí)例分析
- Thinkphp 框架擴(kuò)展之應(yīng)用模式實(shí)現(xiàn)方法分析
- Thinkphp 框架擴(kuò)展之Widget擴(kuò)展實(shí)現(xiàn)方法分析
- Thinkphp 框架擴(kuò)展之行為擴(kuò)展原理與實(shí)現(xiàn)方法分析
- 淺談php常用的7大框架的優(yōu)缺點(diǎn)
相關(guān)文章
PHP網(wǎng)頁(yè)游戲?qū)W習(xí)之Xnova(ogame)源碼解讀(六)
這篇文章主要介紹了PHP網(wǎng)頁(yè)游戲Xnova(ogame)源碼解讀的公共代碼,需要的朋友可以參考下2014-06-06
PHP下的Oracle客戶端擴(kuò)展(OCI8)安裝教程
這篇文章主要介紹了PHP下的Oracle客戶端擴(kuò)展(OCI8)安裝教程,本文在Linux系統(tǒng)中實(shí)現(xiàn),OCI8是用來(lái)連接Oracle數(shù)據(jù)庫(kù)的PHP擴(kuò)展模塊,需要的朋友可以參考下2014-09-09
yii2整合百度編輯器umeditor及umeditor圖片上傳問(wèn)題的解決辦法
這篇文章主要介紹了yii2整合百度編輯器umeditor及umeditor圖片上傳問(wèn)題的解決辦法的相關(guān)資料,需要的朋友可以參考下2016-04-04
PHP中把stdClass Object轉(zhuǎn)array的幾個(gè)方法
PHP和JS通訊通常都用json,但用 json 傳過(guò)來(lái)的數(shù)組并不是標(biāo)準(zhǔn)的array,而是 stdClass 類型。那么我們可以參考下面的幾個(gè)方法進(jìn)行轉(zhuǎn)換。2014-05-05
PHP的openssl加密擴(kuò)展使用小結(jié)(推薦)
下面小編就為大家?guī)?lái)一篇PHP的openssl加密擴(kuò)展使用小結(jié)(推薦)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2016-07-07
CI框架給視圖添加動(dòng)態(tài)數(shù)據(jù)
這篇文章主要介紹了CI框架給視圖添加動(dòng)態(tài)數(shù)據(jù)的相關(guān)資料,需要的朋友可以參考下2014-12-12
使用PHPMyAdmin修復(fù)論壇數(shù)據(jù)庫(kù)的圖文方法
服務(wù)器意外重啟或者斷電、MySQL 不穩(wěn)定等,都有可能引起數(shù)據(jù)表?yè)p壞。本教程簡(jiǎn)單講述如何使用 phpMyAdmin 修復(fù)數(shù)據(jù)表2012-01-01
php更新修改excel中的內(nèi)容實(shí)例代碼
這篇文章主要介紹了php更新修改excel中的內(nèi)容實(shí)例代碼,需要的朋友可以參考下2014-02-02

