PHP實(shí)現(xiàn)的觀察者模式實(shí)例
本文實(shí)例講述了PHP實(shí)現(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();
}
//實(shí)現(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)文章
php5.2的curl-bug 服務(wù)器被php進(jìn)程卡死問題排查
這篇文章主要介紹了php5.2的curl-bug 服務(wù)器被php進(jìn)程卡死問題排查,需要的朋友可以參考下2016-09-09
php cache類代碼(php數(shù)據(jù)緩存類)
php的執(zhí)行效率很高,速度很快,但是連接數(shù)據(jù)庫、查詢數(shù)據(jù)庫等還是比較耗時的。2010-04-04
利用PHP實(shí)現(xiàn)短域名互轉(zhuǎn)
如何使用PHP實(shí)現(xiàn)短域名互轉(zhuǎn)?下面的代碼可以幫助你實(shí)現(xiàn),非常簡單,需要的朋友可以參考下2013-07-07
PHP實(shí)現(xiàn)視頻文件上傳完整實(shí)例
這篇文章主要介紹了PHP實(shí)現(xiàn)視頻文件上傳的技巧,包含了PHP配置信息的設(shè)計及大文件的處理,需要的朋友可以參考下2014-08-08
有關(guān)PHP中MVC的開發(fā)經(jīng)驗(yàn)分享
經(jīng)過近一個月對MVC的研究,自己也通過網(wǎng)上有朋友的指導(dǎo),有了一套自己的MVC流程及框架,但是感覺缺限還是很多,靈活性方面還是欠缺,但又不知道怎么樣的具體改進(jìn),今天我就把我的流程及思想發(fā)布,希望有高人能夠指點(diǎn)指點(diǎn)2012-05-05

