Home arrow PHP arrow Page 3 - Working with MySQL Result Sets and the Decorator Pattern in PHP

Retrieving MySQL result sets without changing the original class - 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.

  1. Working with MySQL Result Sets and the Decorator Pattern in PHP
  2. Handling MySQL result sets
  3. Retrieving MySQL result sets without changing the original class
  4. Defining multiple decorator classes
By: Alejandro Gervasio
Rating: starstarstarstarstar / 12
September 05, 2006

print this article



Essentially, what I plan to do here is generate different output formats, based on the same MySQL result set, by using several decorator classes. However, I have to admit a nearly identical result might be achieved by aggregating some additional methods to the “Result” class that you saw before. Why not do it that way?

Well, I have a good reason for not doing this. I want to keep the scope of the “Result” class limited to handling result sets, without contaminating its source code with methods that deal with formatting them.

As you can see, here the reward is double: you work with completely independent objects, while using decorator classes to apply specific formats to MySQL result sets. Sounds good, doesn't it?

Having justified the real need for creating decorator classes, I’ll show you the first one, which I call “ResultDecorator.” Its definition is as follows:

class ResultDecorator{
    private $resultObj;
    public $resultSet;
    // pass in 'Result' object to the constructor
    public function __construct(Result $resultObj){
        // get result set without modifying the original object
    private function resetResult(){

Regarding the brand new class listed above, you’ll see that it takes up an object of type “Result” and assigns it as a class property. It additionally uses its “resetResult()” method to get a result set, after running a SELECT query.

So, recapitulating, I created a new class, which takes a result set from the respective “Result” class without modifying its original structure. That’s exactly what a decorator class is about! We’re on the right track now, since from this point onward, building decorator classes that generate different outputs from the same result set is a straightforward process.

After having defined the handy “ResultDecorator” class that you learned before, it’s time to go one step further and start creating the pertinent decorator classes, aimed at rendering different output formats, based on the same MySQL result set.

That’s exactly the subject of the upcoming section, therefore click on the link that appears below and keep on reading.

>>> More PHP Articles          >>> More By Alejandro Gervasio

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates


Dev Shed Tutorial Topics: