topical media & game development

talk show tell print

professional-php-04-observable.php / php



  <?php
  abstract class Observable {
  
    private observers = array();
  
    public function addObserver(Observer & observer) {
           array_push(this->observers, observer);
    }
  
    public function notifyObservers() {
           for (i = 0; i < count(this->observers); i++) {
                   widget = this->observers[i];
                   widget->update(this);
           }
       }
  }
  
  class DataSource extends Observable {
  
    private names;
    private prices;
    private years;
  
    function __construct() {
           this->names = array();
           this->prices = array();
           this->years = array();
    }
  
    public function addRecord(name, price, year) {
           array_push(this->names, name);
           array_push(this->prices, price);
           array_push(this->years, year);
           this->notifyObservers();
    }
  
    public function getData() {
           return array(this->names, this->prices, this->years);
    }
  }
  ?>
  


(C) Æliens 20/2/2008

You may not copy or print any of this material without explicit permission of the author or the publisher. In case of other copyright issues, contact the author.