To implement a publish/subscribe behaviour to an object, whenever a "Subject" object changes its state, the attached "Observers" will be notified. It is used to shorten the amount of coupled objects and uses loose coupling instead.
- a message queue system is observed to show the progress of a job in a GUI
PHP already defines two interfaces that can help to implement this pattern: SplObserver and SplSubject.
You can also find this code on GitHub
.. literalinclude:: User.php :language: php :linenos:
.. literalinclude:: UserObserver.php :language: php :linenos:
.. literalinclude:: Tests/ObserverTest.php :language: php :linenos: