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