Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
..
Failed to load latest commit information.
Tests Adapt Unit Tests code to PHPUnit 6 Mar 8, 2017
uml PHP7 Observer Sep 22, 2016
README.rst Improve grammar: these code > this code Apr 4, 2017
User.php PHP7 Observer Sep 22, 2016
UserObserver.php PHP7 Observer Sep 22, 2016

README.rst

Observer

Purpose

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.

Examples

  • a message queue system is observed to show the progress of a job in a GUI

Note

PHP already defines two interfaces that can help to implement this pattern: SplObserver and SplSubject.

UML Diagram

Alt Observer UML Diagram

Code

You can also find this code on GitHub

User.php

.. literalinclude:: User.php
   :language: php
   :linenos:

UserObserver.php

.. literalinclude:: UserObserver.php
   :language: php
   :linenos:

Test

Tests/ObserverTest.php

.. literalinclude:: Tests/ObserverTest.php
   :language: php
   :linenos: