mirror of
https://github.com/DesignPatternsPHP/DesignPatternsPHP.git
synced 2025-02-24 17:52:25 +01:00
50 lines
1011 B
PHP
50 lines
1011 B
PHP
<?php
|
|
|
|
namespace DesignPatterns\Behavioral\Observer;
|
|
|
|
/**
|
|
* User implements the observed object (called Subject), it maintains a list of observers and sends notifications to
|
|
* them in case changes are made on the User object
|
|
*/
|
|
class User implements \SplSubject
|
|
{
|
|
/**
|
|
* @var string
|
|
*/
|
|
private $email;
|
|
|
|
/**
|
|
* @var \SplObjectStorage
|
|
*/
|
|
private $observers;
|
|
|
|
public function __construct()
|
|
{
|
|
$this->observers = new \SplObjectStorage();
|
|
}
|
|
|
|
public function attach(\SplObserver $observer)
|
|
{
|
|
$this->observers->attach($observer);
|
|
}
|
|
|
|
public function detach(\SplObserver $observer)
|
|
{
|
|
$this->observers->detach($observer);
|
|
}
|
|
|
|
public function changeEmail(string $email)
|
|
{
|
|
$this->email = $email;
|
|
$this->notify();
|
|
}
|
|
|
|
public function notify()
|
|
{
|
|
/** @var \SplObserver $observer */
|
|
foreach ($this->observers as $observer) {
|
|
$observer->update($this);
|
|
}
|
|
}
|
|
}
|