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

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

PHP設計模式入門之迭代器模式原理與實現方法分析

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

本文實例講述了PHP設計模式入門之迭代器模式。分享給大家供大家參考,具體如下:

在深入研究這個設計模式之前,我們先來看一道面試題,來自鳥哥的博客,

題目是這樣的:

使對象可以像數組一樣進行foreach循環,要求屬性必須是私有。

不使用迭代器模式很難實現,先看實現的代碼:

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接口來自php的spl類庫,在寫完設計模式的相關文章之后,將會進一步研究這個類庫。

另外在網上找到了一段yii框架中關于迭代器模式的實現代碼:

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

關于迭代器設計模式官方的定義是:使用迭代器模式來提供對聚合對象的統一存取,即提供一個外部的迭代器來對聚合對象進行訪問和遍歷 , 而又不需暴露該對象的內部結構。又叫做游標(Cursor)模式。

好吧,我不是很能理解。為什么明明數組已經可以用foreach來遍歷了還要用這樣一種迭代器模式來實現,只有等待工作經驗的加深來進一步理解吧。

參考文檔:

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

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

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

更多關于PHP相關內容感興趣的讀者可查看本站專題:《php面向對象程序設計入門教程》、《PHP數組(Array)操作技巧大全》、《PHP基本語法入門教程》、《PHP運算與運算符用法總結》、《php字符串(string)用法總結》、《php+mysql數據庫操作入門教程》及《php常見數據庫操作技巧匯總》

希望本文所述對大家PHP程序設計有所幫助。

標簽: PHP
相關文章:
主站蜘蛛池模板: 巨大热杵在腿间进进出出视频 | a级特黄毛片免费观看 | 中国大陆一级毛片 | 性色a| 欧美视频一区二区三区四区 | 伊人久爱 | 国产亚洲精品一区二区在线观看 | 精品综合 | 国产高清在线不卡 | 一级一片一a一片 | 精品久久久久久中文字幕一区 | 成人永久福利在线观看不卡 | 男操女视频 | 亚洲国产二区三区 | 久久中文字幕久久久久91 | 久久精品国产亚洲片 | 99久久精品免费看国产免费 | 久久福利青草精品资源站 | 国产杨幂福利在线视频观看 | 欧美另类精品一区二区三区 | 夜夜春夜夜夜夜猛噜噜噜噜噜 | 久久精品一区二区国产 | 在线看毛片网站 | 日韩三级中文 | 欧美videos娇小 | 北岛玲亚洲一区在线观看 | 欧美成人亚洲综合精品欧美激情 | 久久久99视频| 二区中文字幕 | a级性生活视频 | 中文字幕亚洲精品日韩精品 | 亚洲欧美在线综合一区二区三区 | 97一级毛片全部免费播放 | 国产毛片久久久久久国产毛片 | 天堂一区二区在线观看 | 国内一级野外a一级毛片 | 久久香蕉国产线看观看式 | 欧美视频在线观看免费精品欧美视频 | 一二三区在线观看 | 免费一区区三区四区 | 激情6月丁香婷婷色综合 |