HomePHP Working with MySQL Result Sets and the Decorator Pattern in PHP
Working with MySQL Result Sets and the Decorator Pattern in PHP
If you’re one of those avid PHP developers who wants to learn how to apply the decorator design pattern within your Web applications, then this group of articles might be quite appealing to you. Welcome to the second part of the series “Using the Decorator pattern with PHP.” Comprised of two parts, this series walks you through the core concepts of using this pattern in PHP, and shows you its practical edge with numerous hands-on examples.
For those readers that just found this article after doing some research on the Web, let me tell you that in the previous tutorial, I explained the foundations of the decorator pattern in PHP, and developed an easy-going example to show how it works. Basically, this pattern will allow you to add more functionality to an existing class, without having to change its original structure.
As I mentioned right at the beginning of this series, this result also might be achieved by using inheritance and creating some subclasses from a base class. However, depending on the context where an application will be developed, there are times when it’s preferable to work with objects of different types, instead of using child objects that belong to the same family.
Whatever reasons you have for using objects of different types, the fact is that creating decorator objects in PHP is admittedly easy. In this second article, I’ll take advantage of this handy feature by demonstrating how the decorator pattern can be used in a real situation that you probably tackle very often: processing MySQL result sets.
Definitely, the pattern in question can be used for generating different types of outputs (or views) based on a returned result set, by using a few decorator classes that I’ll define in the course of this tutorial. Hopefully, when you finish reading these lines, you should have a clear idea of how to include decorator classes in your object-based PHP applications.
Are you ready to learn more about the decorator pattern? Okay, let’s get started!