发布时间:2015-05-27 19:19:31作者:知识屋
PHP的观察者设计模式实现相对简单,但是PHP5+版本中已经有标准库类库支持,我们只需简单继承并实现就可以了。
观察者:实现标准接口类库SplSubject。一个注册方法:attach,一个取消注册方法:detach。一个通知方法:nofity。
observers =array(); } public function attach(SplObserver $observer){ $this->observers[] = $observer; } public function detach(SplObserver $observer){ if($idx = array_search($observer, $this->observers,true)) { unset($this->observers[$idx]); } } /** * * Notify observers one by one (main entry) * * @param none * @return none */ public function notify(){ foreach($this->observers as $observer){ $observer->update($this); } } public function setValue($value){ $this->value = $value; //$this->notify(); } public function getValue(){ return $this->value; }}
getValue(); }}
getValue(); }}
attach(new TSPLObserver());$observer1 = new TSPLObserver1();$subject->attach($observer1);//$subject->attach(new TSPLObserver2());//$subject->detach($observer1);$subject->notify();exit();
>php basic.php
The new state of subject
The new state of subject one
如何对PHP文件进行加密方法 PHP实现加密的几种方式介绍
php生成圆角图片的方法 电脑中php怎么生成圆角图片教程
用PHP构建一个留言本方法步骤 php怎么实现留言板功能
php中三元运算符用法 php中的三元运算符使用说明
php文件如何怎么打开方式介绍 php文件用什么打开方法
PHP怎么插入数据库方法步骤 php编程怎么导入数据库教程
如何安装PHPstorm并配置方法教程 phpstorm安装后要进行哪些配置
PHP 获取远程文件大小的3种解决方法 如何用PHP获取远程大文件的大小
20个实用PHP实例代码 php接口开发实例代码详细介绍
如何架设PHP服务器方法步骤 怎么搭建php服务器简单教程