PHP基于SPL實(shí)現(xiàn)的迭代器模式示例
本文實(shí)例講述了PHP基于SPL實(shí)現(xiàn)的迭代器模式。分享給大家供大家參考,具體如下:
現(xiàn)在有這么兩個(gè)類,Department部門類、Employee員工類:
//部門類 class Department{ private $_name; private $_employees; function __construct($name){ $this->_name = $name; $this->employees = array(); } function addEmployee(Employee $e){ $this->_employees[] = $e; echo "員工{$e->getName()}被分配到{$this->_name}中去"; } } //員工類 class Employee{ private $_name; function __construct($name){ $this->_name = $name; } function getName(){ return $this->_name; } } //應(yīng)用: $lsgo = new Department('LSGO實(shí)驗(yàn)室'); $e1 = new Employee("小錦"); $e2 = new Employee("小豬"); $lsgo->addEmployee($e1); $lsgo->addEmployee($e2);
好了,現(xiàn)在LSGO實(shí)驗(yàn)室已經(jīng)有兩個(gè)部員了,現(xiàn)在我想把全部的部員都列出來,就是用循環(huán)來獲取部門的每個(gè)員工的詳情。
在這里我們用PHP中的SPL標(biāo)準(zhǔn)庫提供的迭代器來實(shí)現(xiàn)。
《大話設(shè)計(jì)模式》中如是說:
迭代器模式:迭代器模式是遍歷集合的成熟模式,迭代器模式的關(guān)鍵是將遍歷集合的任務(wù)交給一個(gè)叫做迭代器的對(duì)象,它的工作時(shí)遍歷并選擇序列中的對(duì)象,而客戶端程序員不必知道或關(guān)心該集合序列底層的結(jié)構(gòu)。
迭代器模式的作用簡(jiǎn)而言之:是使所有復(fù)雜數(shù)據(jù)結(jié)構(gòu)的組件都可以使用循環(huán)來訪問
假如我們的對(duì)象要實(shí)現(xiàn)迭代,我們使這個(gè)類實(shí)現(xiàn) Iterator(SPL標(biāo)準(zhǔn)庫提供),這是一個(gè)迭代器接口,為了實(shí)現(xiàn)該接口,我們必須實(shí)現(xiàn)以下方法:
current()
,該函數(shù)返回當(dāng)前數(shù)據(jù)項(xiàng)
key()
,該函數(shù)返回當(dāng)前數(shù)據(jù)項(xiàng)的鍵或者該項(xiàng)在列表中的位置
next()
,該函數(shù)使數(shù)據(jù)項(xiàng)的鍵或者位置前移
rewind()
,該函數(shù)重置鍵值或者位置
valid()
,該函數(shù)返回 bool 值,表明當(dāng)前鍵或者位置是否指向數(shù)據(jù)值
實(shí)現(xiàn)了 Iterator 接口和規(guī)定的方法后,PHP就能夠知道該類類型的對(duì)象需要迭代。
我們使用這種方式重構(gòu) Department 類:
class Department implements Iterator { private $_name; private $_employees; private $_position;//標(biāo)志當(dāng)前數(shù)組指針位置 function __construct($name) { $this->_name = $name; $this->employees = array(); $this->_position = 0; } function addEmployee(Employee $e) { $this->_employees[] = $e; echo "員工{$e->getName()}被分配到{$this->_name}中去"; } //實(shí)現(xiàn) Iterator 接口要求實(shí)現(xiàn)的方法 function current() { return $this->_employees[$this->_position]; } function key() { return $this->_position; } function next() { $this->_position++; } function rewind() { $this->_position = 0; } function valid() { return isset($this->_employees[$this->_position]); } } //Employee 類同前 //應(yīng)用: $lsgo = new Department('LSGO實(shí)驗(yàn)室'); $e1 = new Employee("小錦"); $e2 = new Employee("小豬"); $lsgo->addEmployee($e1); $lsgo->addEmployee($e2); echo "LSGO實(shí)驗(yàn)室部員情況:"; //這里其實(shí)遍歷的$_employee foreach($lsgo as $val){ echo "部員{$val->getName()}"; }
附加:
假如現(xiàn)在我們想要知道該部門有幾個(gè)員工,如果是數(shù)組的話,一個(gè) count()
函數(shù)就 ok 了,那么我們能不能像上面那樣把對(duì)象當(dāng)作數(shù)組來處理?SPL標(biāo)準(zhǔn)庫中提供了 Countable 接口供我們使用:
class Department implements Iterator,Countable{ //前面同上 //實(shí)現(xiàn)Countable中要求實(shí)現(xiàn)的方法 function count(){ return count($this->_employees); } } //應(yīng)用: echo "員工數(shù)量:"; echo count($lsgo);
本文參考自《深入理解PHP高級(jí)技巧、面向?qū)ο笈c核心技術(shù)》
更多關(guān)于PHP相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《php面向?qū)ο蟪绦蛟O(shè)計(jì)入門教程》、《PHP數(shù)組(Array)操作技巧大全》、《PHP基本語法入門教程》、《PHP運(yùn)算與運(yùn)算符用法總結(jié)》、《php字符串(string)用法總結(jié)》、《php+mysql數(shù)據(jù)庫操作入門教程》及《php常見數(shù)據(jù)庫操作技巧匯總》
希望本文所述對(duì)大家PHP程序設(shè)計(jì)有所幫助。
- PHP設(shè)計(jì)模式之迭代器模式Iterator實(shí)例分析【對(duì)象行為型】
- php設(shè)計(jì)模式之迭代器模式實(shí)例分析【星際爭(zhēng)霸游戲案例】
- PHP設(shè)計(jì)模式之迭代器(Iterator)模式入門與應(yīng)用詳解
- PHP迭代器和生成器用法實(shí)例分析
- php和C#的yield迭代器實(shí)現(xiàn)方法對(duì)比分析
- PHP設(shè)計(jì)模式之PHP迭代器模式講解
- PHP迭代器和迭代的實(shí)現(xiàn)與使用方法分析
- PHP聚合式迭代器接口IteratorAggregate用法分析
- PHP迭代器接口Iterator用法分析
- PHP迭代器的內(nèi)部執(zhí)行過程詳解
- PHP設(shè)計(jì)模式之迭代器模式的深入解析
- PHP中迭代器的簡(jiǎn)單實(shí)現(xiàn)及Yii框架中的迭代器實(shí)現(xiàn)方法示例
相關(guān)文章
解決Laravel blade模板轉(zhuǎn)義html標(biāo)簽的問題
今天小編就為大家分享一篇解決Laravel blade模板轉(zhuǎn)義html標(biāo)簽的問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2019-09-09PHP正則替換函數(shù)preg_replace()報(bào)錯(cuò):Notice Use of undefined constant的解
這篇文章主要介紹了PHP正則替換函數(shù)preg_replace()報(bào)錯(cuò):Notice Use of undefined constant的解決方法,結(jié)合具體實(shí)例形式分析了preg_replace()報(bào)錯(cuò)的原因與相關(guān)解決技巧,需要的朋友可以參考下2017-02-02php實(shí)現(xiàn)encode64編碼類實(shí)例
這篇文章主要介紹了php實(shí)現(xiàn)encode64編碼類,實(shí)例分析了php實(shí)現(xiàn)encode64編碼的技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-03-03