Example #1
0
    }
    /**
     *
     *
     * @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 $name
     * @param $value
     * @return void
     */
    public function __set($name, $value)
    {
        $this->_data[$name] = $value;
        // notify the observers, that user has been updated
        $this->notify();
    }
}
$user = new User();
$user->attach(new UserObserver());
$user->notify();