Dominik Liebler 644d9cbd49 cs
2013-09-13 12:00:39 +02:00

85 lines
1.6 KiB
PHP

<?php
namespace DesignPatterns\Observer;
/**
* Observer pattern : The observed object (the subject)
*
* The subject maintains a list of Observers and sends notifications.
*
*/
class User implements \SplSubject
{
/**
* user data
*
* @var array
*/
protected $data = array();
/**
* observers
*
* @var array
*/
protected $observers = array();
/**
* attach a new observer
*
* @param \SplObserver $observer
*
* @return void
*/
public function attach(\SplObserver $observer)
{
$this->observers[] = $observer;
}
/**
* detach an observer
*
* @param \SplObserver $observer
*
* @return void
*/
public function detach(\SplObserver $observer)
{
$index = array_search($observer, $this->observers);
if (false !== $index) {
unset($this->observers[$index]);
}
}
/**
* notify observers
*
* @return void
*/
public function notify()
{
/** @var SplObserver $observer */
foreach ($this->observers as $observer) {
$observer->update($this);
}
}
/**
* Ideally one would better write setter/getter for all valid attributes and only call notify()
* on attributes that matter when changed
*
* @param string $name
* @param mixed $value
*
* @return void
*/
public function __set($name, $value)
{
$this->data[$name] = $value;
// notify the observers, that user has been updated
$this->notify();
}
}