O'Reilly logo

Learning PHP Design Patterns by William Sanders

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Chapter 14. Building a Multidevice CMS with the Observer Pattern

If there were observers on Mars, they would probably be amazed that we have survived this long.

Noam Chomsky

In dream consciousness... we make things happen by wishing them, because we are not only the observer of what we experience but also the creator.

Pir Vilayat Khan

Knowledge is what we get when an observer, preferably a scientifically trained observer, provides us with a copy of reality that we can all recognize.

Christopher Lasch

Built-In Observer Interfaces

One of the many treats found within PHP 5.1.0 and up is a set of interfaces that can be used with the Observer design pattern. This chapter will look at an Observer pattern beginning from scratch without any built-in features, but to get started we’ll have a short overview of the SplObserver interface along with the SplSubject and the SplObjectStorage interfaces that make building an Observer pattern with PHP a snap. “SPL” is shorthand for Standard PHP Library and consists of a collection of interfaces and classes used to solve standard problems.

However, before we get ahead of ourselves, you need some idea of what the Observer pattern is and what it does. Fortunately, its class diagram is quite detailed, and many of the features prized in the Model-View-Controller (MVC) pattern can be found in the Observer. (You might even think of the pattern as an alternative to the MVC.) At the root of the Observer are Subject and Observer interfaces. The Subject holds a ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required