Exemplo n.º 1
0
    }
    public function fire()
    {
        //perform login
        $this->notify();
    }
}
class LogHandler implements Observer
{
    public function handle()
    {
        var_dump('log something important');
    }
}
class EmailNotifier implements Observer
{
    public function handle()
    {
        var_dump('fire off an email');
    }
}
class LoginReporter implements Observer
{
    public function handle()
    {
        var_dump('do some form of reporting');
    }
}
$login = new Login();
$login->attach([new LogHandler(), new EmailNotifier(), new LoginReporter()]);
$login->fire();
Exemplo n.º 2
0
/**
 * 登陆后发邮件
 */
class LoginAndEmail implements Observer
{
    /**
     * 观察者动作
     */
    public function handle()
    {
        var_dump("login and emailing...");
    }
}
/**
 * 登陆后推送信息
 */
class LoginAndNotify implements Observer
{
    /**
     * 观察者动作
     */
    public function handle()
    {
        var_dump("login and notifying...");
    }
}
$login = new Login();
$email = new LoginAndEmail();
$notify = new LoginAndNotify();
$login->attach([$email, $notify]);
$login->doLogin();
Exemplo n.º 3
0
 function __construct(Login $login)
 {
     $this->login = $login;
     $login->attach($this);
 }
Exemplo n.º 4
0
 */
class LogHandler implements Observer
{
    public function handle()
    {
        var_dump("Log something");
    }
}
/**
 * Class EmailHandler
 */
class EmailHandler implements Observer
{
    public function handle()
    {
        var_dump('Send an email');
    }
}
/**
 * Class EmailHandler
 */
class Reporter implements Observer
{
    public function handle()
    {
        var_dump('Reporting');
    }
}
$login = new Login();
$login->attach([new LogHandler(), new EmailHandler()])->attach(new Reporter());
$login->fire();
Exemplo n.º 5
0
            return $o != $observer;
        });
    }
    public function notify()
    {
        return array_walk($this->observers, function ($observer) {
            $observer->handle();
        });
    }
}
class SendWelcomeEmail implements Observer
{
    public function handle()
    {
        var_dump('send a welcome email.');
    }
}
class UpdateAccumulateScore implements Observer
{
    public function handle()
    {
        var_dump('update the accumulate score.');
    }
}
$login = new Login();
$sendWelcomeEmail = new SendWelcomeEmail();
$login->attach($sendWelcomeEmail);
$login->attach(new UpdateAccumulateScore());
$login->notify();
$login->detach($sendWelcomeEmail);
$login->notify();
        unset($this->observers[$index]);
    }
    public function notify()
    {
        foreach ($this->observers as $observer) {
            $observer->handle();
        }
    }
    public function fire()
    {
        $this->notify();
    }
}
class LogHandler implements Observer
{
    public function handle()
    {
        var_dump('log something important.');
    }
}
class EmailHandler implements Observer
{
    public function handle()
    {
        var_dump('email something.');
    }
}
$login = new Login();
$login->attach(new LogHandler());
$login->attach(new EmailHandler());
$login->fire();
Exemplo n.º 7
0
Arquivo: Demo.php Projeto: jzfan/test
        foreach ($this->observers as $observer) {
            $observer->handle();
        }
    }
    private function attachObservers($observers)
    {
        foreach ($observers as $observer) {
            if (!$observer instanceof Observer) {
                throw new Exception();
            }
            $this->attach($observer);
        }
    }
}
class Log implements Observer
{
    public function handle()
    {
        echo 'log something about login event' . '<br>';
    }
}
class Email implements Observer
{
    public function handle()
    {
        echo 'fire off an email' . '<br>';
    }
}
$login = new Login();
$login->attach([new Log(), new Email()]);
$login->notify();