Skip to content

Instantly share code, notes, and snippets.

@frankmayer
Forked from boxers999/gist:1216045
Created October 26, 2013 17:30

Revisions

  1. @boxers999 boxers999 revised this gist Sep 14, 2011. 1 changed file with 6 additions and 1 deletion.
    7 changes: 6 additions & 1 deletion gistfile1.aw
    Original file line number Diff line number Diff line change
    @@ -13,7 +13,7 @@ class DemoSubject implements SplSubject{
    }

    public function detach(SplObserver $observer){
    if($idx = array_search($observer, $this->observers, true)){
    if(($idx = array_search($observer, $this->observers, true)) !== false){
    unset($this->observers[$idx]);
    }
    }
    @@ -46,3 +46,8 @@ $subject = new DemoSubject();
    $observer = new DemoObserver();
    $subject->attach($observer);
    $subject->setValue(5);
    $subject->detach($observer);
    $subject->setValue(10);



  2. @boxers999 boxers999 created this gist Sep 14, 2011.
    48 changes: 48 additions & 0 deletions gistfile1.aw
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,48 @@
    <?php

    class DemoSubject implements SplSubject{

    private $observer, $value;

    public function __construct(){
    $this->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]);
    }
    }

    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;
    }
    }

    class DemoObserver implements SplObserver {

    public function update(SplSubject $subject){
    echo 'This is the value :- '. $subject->getValue();
    }

    }

    $subject = new DemoSubject();
    $observer = new DemoObserver();
    $subject->attach($observer);
    $subject->setValue(5);