PHP
  Home arrow PHP arrow Page 3 - Working with MySQL Result Sets and the Decorator Pattern in PHP
Dev Shed Forums  
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
VPS Hosting  
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid  
Request Media Kit
Contact Us  
Site Map  
Privacy Policy  
Support  
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
Google.com  
PHP

Working with MySQL Result Sets and the Decorator Pattern in PHP
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 12
    2006-09-05


    Table of Contents:
  • Working with MySQL Result Sets and the Decorator Pattern in PHP
  • Handling MySQL result sets
  • Retrieving MySQL result sets without changing the original class
  • Defining multiple decorator classes

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article

     
     
    ADVERTISEMENT


    Working with MySQL Result Sets and the Decorator Pattern in PHP - Retrieving MySQL result sets without changing the original class
    ( Page 3 of 4 )

    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){
            $this->resultObj=$resultObj;
            // get result set without modifying the original object
            $this->resetResult();
        }
        private function resetResult(){
            $this->resultSet=$this->resultObj->getResult();
        }
    }

    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
     

       

    PHP ARTICLES

    - Implementing Factory Methods in PHP 5
    - Merging a File Split for FTP Upload using PHP
    - Getting Data from Yahoo Site Explorer Inboun...
    - Method Chaining: Adding More Selecting Metho...
    - How to Split a File During an FTP Upload Usi...
    - Expanding a Custom CodeIgniter Library with ...
    - Using the Yahoo Site Explorer Inbound Links ...
    - Building a CodeIgniter Custom Library with M...
    - Building an E-mini Trading System Using PHP ...
    - Completing the MySQL Class with Method Chain...
    - Building Dynamic Queries with Chainable Meth...
    - PHP Encryption and Decryption Methods
    - Building a MySQL Abstraction Class with Meth...
    - Completing a Sample String Processor with Me...
    - Mastering WHILE Loops for PHP and MySQL





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 6 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek