Advertisement
Guest User

Rune Kaagaard

a guest
Oct 14th, 2010
196
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.54 KB | None | 0 0
  1. <?php
  2.  
  3. error_reporting(E_ALL|E_STRICT);
  4.  
  5. class RuneSubject implements SplSubject {
  6.     private $_observers;
  7.  
  8.     public function __construct() {
  9.         $this->_observers = new SplObjectStorage();
  10.     }
  11.  
  12.     public function attach(SplObserver $object) {
  13.         $this->_observers->attach($object);
  14.     }
  15.  
  16.     public function detach(SplObserver $object) {
  17.         $this->_observers->detach($object);
  18.     }
  19.  
  20.     public function notify() {
  21.         foreach ($this->_observers as $o) $o->update($this);
  22.     }
  23. }
  24. class RuneAccount extends RuneSubject  {
  25.     public $status;
  26.    
  27.     public function __construct() {
  28.         parent::__construct();
  29.         $this->attach(new RuneLogger());
  30.         $this->attach(new RuneMailer());
  31.     }
  32.  
  33.     public function save() {
  34.         $this->notify();
  35.     }
  36. }
  37.  
  38. class RuneLogger implements SplObserver {
  39.     public function update(SplSubject $subject) {
  40.         //Update status in log table
  41.         echo "Updating status in log table.\n";
  42.     }
  43. }
  44.  
  45. class RuneMailer implements SplObserver {
  46.     public function update(SplSubject $subject) {
  47.         switch (get_class($subject)) {
  48.             case "RuneAccount":
  49.                 if ($subject->status == "Expired") {
  50.                     //send email: "account expired"
  51.                     echo "Sending account expired email.\n";
  52.                 }
  53.         }
  54.     }
  55. }
  56.  
  57. $account = new RuneAccount();
  58. $account->status = "Expired";
  59. $account->save();
  60.  
  61. /**
  62.  * Outputs:
  63.  *   Updating status in log table
  64.  *   Sending account expired email.
  65.  */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement