29.4. Observer

Observer is one of the most useful design patterns for developing large-scale object-oriented applications. It allows you, with the use of messages, to interconnect objects without their having to know anything about each other. At the heart of the Observer pattern are two main actors: observers and subjects. Observer objects find subject objects interesting and need to know when the subject changes. Typically, multiple observers monitor a single subject.

Listing 29.3 contains a simple implementation of the Observer pattern.

Listing 29.3. Observer pattern
 <?php interface Message { static function getType(); }; interface Observer { function notifyMsg(Message $msg); }; class Subject { private $observers = array(); function registerObserver(Observer ...

Get Core PHP Programming, Third Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.