The Observer design pattern essentially allows an object (the subject) to maintain a list of observers that are automatically notified when the state of the that object changes.
This pattern applies a one-to-many dependency between objects; there is always one subject that updates many observers.
The Gang of Four originally identified that this pattern was particularly applicable in cases where an abstraction has two aspects, with one dependent on the other. In addition to this, it is very useful when a change to object requires changes to the others and you don't know how many other objects need to be changed. Finally, this pattern is also incredibly useful when an object should notify other objects without making assumptions about what those objects are, thus making this pattern great for loosely coupling this relationship.
PHP provides a very useful interface called SplObserver
and SplSubject
. These interfaces provide the template for implementing...