成人视屏在线观看-国产99精品-国产精品1区2区-欧美一级在线观看-国产一区二区日韩-色九九九

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

PHP設(shè)計(jì)模式入門(mén)之迭代器模式原理與實(shí)現(xiàn)方法分析

瀏覽:161日期:2022-09-10 16:18:07

本文實(shí)例講述了PHP設(shè)計(jì)模式入門(mén)之迭代器模式。分享給大家供大家參考,具體如下:

在深入研究這個(gè)設(shè)計(jì)模式之前,我們先來(lái)看一道面試題,來(lái)自鳥(niǎo)哥的博客,

題目是這樣的:

使對(duì)象可以像數(shù)組一樣進(jìn)行foreach循環(huán),要求屬性必須是私有。

不使用迭代器模式很難實(shí)現(xiàn),先看實(shí)現(xiàn)的代碼:

sample.php

<?phpclass Sample implements Iterator{ private $_arr; public function __construct(Array $arr){ $this->_arr = $arr; } public function current(){ return current($this->_arr); } public function next(){ return next($this->_arr); } public function key(){ return key($this->_arr); } public function valid(){ return $this->current() !== false; } public function rewind(){ reset($this->_arr); }}

index.php

<?phprequire ’Sample.php’; $arr = new Sample([’max’, ’ben’, ’will’]); foreach ($arr as $k=>$v){ echo $k.'-'.$v.'<br />';}

其中Iterator接口來(lái)自php的spl類(lèi)庫(kù),在寫(xiě)完設(shè)計(jì)模式的相關(guān)文章之后,將會(huì)進(jìn)一步研究這個(gè)類(lèi)庫(kù)。

另外在網(wǎng)上找到了一段yii框架中關(guān)于迭代器模式的實(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 />’;}

關(guān)于迭代器設(shè)計(jì)模式官方的定義是:使用迭代器模式來(lái)提供對(duì)聚合對(duì)象的統(tǒng)一存取,即提供一個(gè)外部的迭代器來(lái)對(duì)聚合對(duì)象進(jìn)行訪問(wèn)和遍歷 , 而又不需暴露該對(duì)象的內(nèi)部結(jié)構(gòu)。又叫做游標(biāo)(Cursor)模式。

好吧,我不是很能理解。為什么明明數(shù)組已經(jīng)可以用foreach來(lái)遍歷了還要用這樣一種迭代器模式來(lái)實(shí)現(xiàn),只有等待工作經(jīng)驗(yàn)的加深來(lái)進(jìn)一步理解吧。

參考文檔:

https://www.jb51.net/article/184182.htm

https://www.jb51.net/article/185478.htm

https://www.jb51.net/article/185483.htm

更多關(guān)于PHP相關(guān)內(nèi)容感興趣的讀者可查看本站專(zhuān)題:《php面向?qū)ο蟪绦蛟O(shè)計(jì)入門(mén)教程》、《PHP數(shù)組(Array)操作技巧大全》、《PHP基本語(yǔ)法入門(mén)教程》、《PHP運(yùn)算與運(yùn)算符用法總結(jié)》、《php字符串(string)用法總結(jié)》、《php+mysql數(shù)據(jù)庫(kù)操作入門(mén)教程》及《php常見(jiàn)數(shù)據(jù)庫(kù)操作技巧匯總》

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

標(biāo)簽: PHP
相關(guān)文章:
主站蜘蛛池模板: 国产大陆亚洲精品国产 | www操操操 | 日韩免费精品一级毛片 | 久草视频在 | 99色在线视频 | 国产精品观看在线亚洲人成网 | 午夜性色福利视频在线视频 | 天天噜夜夜操 | 欧洲freexxxx性 | a级片在线观看免费 | 黄色三级免费网站 | 精品国产日韩亚洲一区二区 | 国产视频软件在线 | 免费观看亚洲 | 亚欧人成精品免费观看 | 国产一区二区三区免费大片天美 | 成人午夜| 欧美一级毛片欧美毛片视频 | 美女被躁免费视频软件 | 成人18免费入口 | 嫩草影院ncyy在线观看 | 成人亚洲精品7777 | 在线91精品国产免费 | 国产午夜免费视频片夜色 | 欧美成人在线免费观看 | 国产91久久精品 | 一区二区不卡久久精品 | 免费一区二区三区久久 | 18免费网站 | 性久久久久久久久久 | 国产手机看片 | 日本不卡一二三区 | 四虎免费大片aⅴ入口 | 国产超清在线观看 | 日韩国产成人精品视频 | 亚洲精品成人久久久影院 | 久久精品国产只有精品6 | 男女乱配视频免费观看 | 日韩一级a毛片欧美区 | 久艹精品 | 欧美线在线精品观看视频 |