PHP实现的观察者模式实例

yipeiwu_com6年前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中strlen 函数

strlen函数说明。 int strlen ( string $string ) 在这篇文章,我们可以知道strlen函数是通过Zend Engine定义的。函数的定义可以在这里查看...

php查询whois信息的方法

本文实例讲述了php查询whois信息的方法。分享给大家供大家参考。具体如下: 这里使用php通过查询whois信息的网站列表进行查询 function whois_query($d...

PHP数组操作简单案例分析

PHP数组操作简单案例分析

本文实例讲述了PHP数组操作相关技巧。分享给大家供大家参考,具体如下: 这个是一道简单的PHP数组入门题 $Str = "as5454654%^$%^$7675dhasjkdhh12...

初级的用php写的采集程序

可以先用这个采集然后在用帝国处理 <?php ###################################################################...

使用PHP实现生成HTML静态页面

从PHP生成HTML静态页面并存储到以年份和月份为名称创建的目录。 读取全部数据批量生成,全部生成后弹出提示。 可指定批次生成数量,建议不超过800,否则执行速度会有问题。 (出于众所周...