如何在PHP項目中實現(xiàn)觀察者模式?針對這個問題,這篇文章詳細(xì)介紹了相對應(yīng)的分析和解答,希望可以幫助更多想解決這個問題的小伙伴找到更簡單易行的方法。
具體如下:
<?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項目中實現(xiàn)觀察者模式問題的解答就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,如果你還有很多疑惑沒有解開,可以關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道了解更多相關(guān)知識。
標(biāo)題名稱:如何在PHP項目中實現(xiàn)觀察者模式-創(chuàng)新互聯(lián)
當(dāng)前網(wǎng)址:http://chinadenli.net/article42/dhjiec.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站維護、網(wǎng)站營銷、全網(wǎng)營銷推廣、網(wǎng)站設(shè)計、Google、網(wǎng)站收錄
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容