...
<?php /* The observer pattern implements a one-too-many dependency between objects. The object that holds the list of dependencies is called subject, while the dependents are called observers. When the subject object changes state, all of the dependents are notified and updated automatically. */ class Customer implements \SplSubject { protected $data = array(); protected $observers = array(); public function attach(\SplObserver $observer) { $this->observers[] = $observer; } public function detach(\SplObserver $observer) { $index = array_search($observer, $this->observers); if ($index !== false) { unset($this->observers[$index]); } } public function notify() { foreach ($this->observers as $observer) { $observer->update($this); echo 'Customer_observer updated.<br/>'; } } public function __set($name, $value) { $this->data[$name] = $value; $this->notify(); } } class CustomerObserver implements \SplObserver { public function update(\SplSubject $subject) { echo "CustomerObserver_update.<br/>"; } } $user = new Customer; $customerObserver = new CustomerObserver(); $user->attach($customerObserver); $user->name = 'John Doe'; $user->email = '[email protected]'; ?>