O'Reilly logo

Professional PHP Design Patterns by Aaron Saray

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

2.2. The Standard PHP Library

The Standard PHP Library (SPL), developed by Marcus Boerger, is a useful collection of interfaces and classes bundled with PHP as an extension. In PHP 5, this was added as part of the core distribution. The manual states that this set of interfaces and classes is "meant to solve standard problems." At first read, this echoes the explanation given earlier of Design Patterns. Indeed, the library has an assortment of features that does help with your creation of Design Patterns in PHP. Let's review a few of the main players of the SPL to see what you may use in your future pattern-based architecture.

2.2.1. SPL Observer and SPL Subject

The SplObserver and the SplSubject interfaces are an implementation of the Observer Design Pattern. SplSubject contains three method declarations: attach(), detach() and notify(). attach(), and detach() are used to attach objects to the chain of notification and receive a SplObserver object. The notify() function is called whenever an action happens that the chain should be aware of. The SplObserver interface contains one method, update(), which receives only one parameter — an SplSubject object. This should be called whenever the SplSubject's value changes.

This may seem very vague and confusing if you're not familiar with the Observer Design Pattern. However, the main thing to remember is the existence of these methods as you continue on to study the Design Patterns. You may find that using some of these methods may ...

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