The NetTuts.com site has continued their series looking at design patterns and how they can be used in PHP. In this new post they focus in on the Decorator pattern, most commonly used to add functionality to a existing class (to "decorate" it).
Earlier in this series we explored both the facade and adapter design patterns in this series. Using facade, we can simplify large systems, and by implementing adapter we can stay safe while working with external API and classes. Now we are going to cover the decorator design pattern, which also falls under the category of structural patterns. We can use the decorator pattern when we just want to give some added responsibility to our base class. This design pattern is a great alternative to a sub‑classing feature for extending functionality with some added advantages.
They start with a problem that needs solving - sending an email with additional content not defined in the parent class. They show how to do something similar with child classes, but quickly find a limitation. Instead, they show how to use decorator classes and a simple interface to provide interchangeable classes that augment the contents of the email body as passed in via constructor injection.