Exemplo n.º 1
0
//============================
class KillBot implements SplObserver
{
    public function update(SplSubject $subject)
    {
        echo __CLASS__ . " says kill all humans." . PHP_EOL;
    }
}
class LoveBot implements SplObserver
{
    public function update(SplSubject $subject)
    {
        echo __CLASS__ . " says kiss all humans." . PHP_EOL;
    }
}
// load the observable (SPLSubject)
$robots = new Observable();
// load some observers
$killbot = new KillBot();
$lovebot = new LoveBot();
// add the observers to the observable
$robots->addObserver($killbot);
$robots->addObserver($lovebot);
// notify the observers of an event
$robots->notify();
/*
Observers output:

KillBot says kill all humans
LoveBot says kiss all humans
*/