Example #1
0
        echo '积分加' . $integral . '<br />';
    }
}
class Observer1 implements SplObserver
{
    public function update(SplSubject $subject)
    {
        $subject->setCount(1);
    }
}
class Observer2 implements SplObserver
{
    public function update(SplSubject $subject)
    {
        $subject->setIntegral(10);
    }
}
//客户端
class Client
{
    public static function test()
    {
        $subject = new Subject();
        $subject->attach(new Observer1());
        $subject->attach(new Observer2());
        $subject->post();
        //输出:数据量加1 积分加10
    }
}
Client::test();
Example #2
0
class Client
{
    private $obj1 = null;
    private $obj2 = null;
    private $sub = null;
    public function __construct()
    {
        $this->obj1 = new ObserverA();
        $this->obj2 = new ObserverB();
        $this->sub = new SubjectA();
    }
    public function test()
    {
        $this->sub->attach($this->obj1);
        $this->sub->attach($this->obj2);
        $this->sub->setState("working");
        $this->sub->detach($this->ob2);
        $this->sub->setState("studying");
        $this->sub->detach($this->obj1);
        $this->sub->setState("eating");
    }
    public function __destruct()
    {
        $obj1 = null;
        $obj2 = null;
        $obj3 = null;
    }
}
$client = new Client();
$client->test();
$client = null;
 function fn(Client $client)
 {
     $this->client = $client;
     $this->client->test();
 }