PHP实现的观察者模式实例

yipeiwu_com5年前PHP代码库

本文实例讲述了PHP实现的观察者模式。分享给大家供大家参考,具体如下:

<?php
  //定义观察者调用接口
  class transfer{
    protected $_observers = array();
    //注册对象
    public function register($sub){
      $this->_observers[] = $sub;
    }
    //外部统一调用
    public function trigger(){
      if(!empty($this->_observers)){
        foreach($this->_observers as $observer){
          $observer->update();
        }
      }
    }
  }
  //观察者接口
  interface obserable{
    public function update();
  }
  //实现观察者
  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();

更多关于PHP相关内容感兴趣的读者可查看本站专题:《php面向对象程序设计入门教程》、《PHP基本语法入门教程》、《PHP网络编程技巧总结》、《PHP数组(Array)操作技巧大全》、《php字符串(string)用法总结》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总

希望本文所述对大家PHP程序设计有所帮助。

相关文章

用php实现让页面只能被百度gogole蜘蛛访问的方法

普通用户与搜索引擎蜘蛛爬行的区别在于发送的user agent,看网站日志文件能发现百度蜘蛛名字包含Baiduspider, 而google的则是Googlebot, 这样我们可以通过判...

JavaScript实现滚动栏效果的方法

本文实例讲述了JavaScript实现滚动栏效果的方法。分享给大家供大家参考。具体如下: <!DOCTYPE html> <html> <head...

PHP使用curl模拟post上传及接收文件的方法

本文实例讲述了PHP使用curl模拟post上传及接收文件的方法。分享给大家供大家参考,具体如下: public function Action_Upload(){ $th...

深入eAccelerator与memcached的区别详解

eAccelerator和memcached,是目前较为主流的两个可使用在PHP之中的缓存加速工具.eAccelerator专门为PHP开发,而memcached不仅仅用在PHP之中,其...

CentOS下PHP安装Oracle扩展

环境 System:CentOS 6 PHP: 5.3.28 下载Oracle客户端 32位系统 64位系统 复制代码 代码如下: oracle-instantclient-sql...