Observer pattern (SplObserver/SplSubject)

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 ...

Get Mastering PHP Design Patterns now with the O’Reilly learning platform.

O’Reilly members experience live online training, plus books, videos, and digital content from nearly 200 publishers.