PHP實現(xiàn)的觀察者模式實例
本文實例講述了PHP實現(xiàn)的觀察者模式。分享給大家供大家參考,具體如下:
<?php //定義觀察者調(diào)用接口 class transfer{ protected $_observers = array(); //注冊對象 public function register($sub){ $this->_observers[] = $sub; } //外部統(tǒng)一調(diào)用 public function trigger(){ if(!empty($this->_observers)){ foreach($this->_observers as $observer){ $observer->update(); } } } } //觀察者接口 interface obserable{ public function update(); } //實現(xiàn)觀察者 class listen implements obserable{ public function update(){ echo 'now first time you need to do listen<br/>'; } } class read implements obserable{ public function update(){ echo 'now first time you need to read<br/>'; } } class speak implements obserable{ public function update(){ echo 'now first time you need to speak<br/>'; } } class write implements obserable{ public function update(){ echo 'now first time you need to write<br/>'; } } $transfer = new transfer(); $transfer->register(new listen()); $transfer->register(new read()); $transfer->register(new speak()); $transfer->register(new write()); $transfer->trigger();
更多關(guān)于PHP相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《php面向?qū)ο蟪绦蛟O(shè)計入門教程》、《PHP基本語法入門教程》、《PHP網(wǎng)絡(luò)編程技巧總結(jié)》、《PHP數(shù)組(Array)操作技巧大全》、《php字符串(string)用法總結(jié)》、《php+mysql數(shù)據(jù)庫操作入門教程》及《php常見數(shù)據(jù)庫操作技巧匯總》
希望本文所述對大家PHP程序設(shè)計有所幫助。
相關(guān)文章
從MySQL數(shù)據(jù)庫表中取出隨機數(shù)據(jù)的代碼
這個例子是用于一個簡單的應(yīng)用開發(fā)了,意思就是把現(xiàn)在表中的所有數(shù)據(jù)我們隨機讀出來一次之后再進行隨機保存到另一個表,從而達到了記錄隨機的功能2007-09-09PHP中IP地址與整型數(shù)字互相轉(zhuǎn)換詳解
這篇文章主要介紹了PHP中IP地址與整型數(shù)字互相轉(zhuǎn)換詳解,本文介紹了使用PHP函數(shù)ip2long與long2ip的使用,以及它們的BUG介紹,最后給出自己寫的兩個算法,需要的朋友可以參考下2014-08-08php模式設(shè)計之觀察者模式應(yīng)用實例分析
這篇文章主要介紹了php模式設(shè)計之觀察者模式,結(jié)合實例形式分析了php觀察者模式的原理、應(yīng)用案例與相關(guān)操作注意事項,需要的朋友可以參考下2019-09-09php使用指定編碼導(dǎo)出mysql數(shù)據(jù)到csv文件的方法
這篇文章主要介紹了php使用指定編碼導(dǎo)出mysql數(shù)據(jù)到csv文件的方法,涉及php查詢mysql及操作csv文件的技巧,具有一定參考借鑒價值,需要的朋友可以參考下2015-03-03