© Kelt Dockins 2017

Kelt Dockins, Design Patterns in PHP and Laravel, 10.1007/978-1-4842-2451-9_22

22. Observer

Kelt Dockins

(1)Dolph, Arkansas, USA

$> git checkout observer

Intent

Define a one-to-many dependency between objects so that when one object changes state, all its dependents are notified and updated automatically.1

Applications

The observer pattern is used when you want objects to react to specific events on a subject object. When the subject changes, it notifies observers. This is one of the most popular patterns I will cover. In fact, PHP already has built-in interfaces for the observer pattern: SplSubject 2 and SplObserver 3.

Abstract Structure

Figure 22-1. Abstract structure
  • SplSubjectis an abstract class or interface. When using ...

Get Design Patterns in PHP and Laravel 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.