PHP
  Home arrow PHP arrow Page 4 - Generating Outputs from MySQL with Static Members and Methods in PHP 5
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

Generating Outputs from MySQL with Static Members and Methods in PHP 5
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 4
    2006-10-09


    Table of Contents:
  • Generating Outputs from MySQL with Static Members and Methods in PHP 5
  • Working with MySQL
  • Creating the ResultToString, ResultToXML and ResultToArray classes
  • Defining the ResultProcessorFactory class

  • 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


    Generating Outputs from MySQL with Static Members and Methods in PHP 5 - Defining the ResultProcessorFactory class
    ( Page 4 of 4 )

    As I said right at the beginning of this series, static methods let you perform specific tasks, without having to be restricted by the existence of a concrete class instance. In this particular case, I’m going to apply that concept to define a factory class, which will be responsible for fabricating result set processor objects.

    Having said that, here is the corresponding definition for the entirely new “ResultProcessorFactory” class:

    // define 'ResultProcessorFactory' class class ResultProcessorFactory{ static public function createResultProcessor($resultProcessor,
    Result $result){        if(!preg_match
    ("/(ResultToString|ResultToXML|ResultToArray)/",$resultProcessor)){ throw new Exception('Invalid result
    processor'); } return new $resultProcessor($result); } }

     

    Undoubtedly, the class coded above won’t blow your mind away, since its signature is very simple. Basically, all that this class does is take the name of the result processor object being created and return this object to client code. Of course, this process is carried out by calling the long-awaited “createResultProcessor()” method, which has been defined as static.

    In addition, it should be noticed that the method also accepts an object of type “Result,” which will be required further to create the appropriate result processor object.

    Now that you know how the previous class looks, I’d like to show you a quick example that illustrates the complete process for generating different outputs from a specific MySQL result set, as well as for invoking the static “createResultProcessor()” method.

    To begin with, suppose that you have a simple CUSTOMERS database table that has been populated with the following data:

    1  Customer 1 customer1@domain.com

    2  Customer 2 customer2@domain.com
    3  Customer 3 customer3@domain.com
    4  Customer 4 customer4@domain.com
    5  Customer 5 customer5@domain.com

    After filling in the sample database table with some basic information about hypothetical customers, please examine the script below, which uses the static “createResultProcessor()” method to generate different views from the respective database table:

    try{

        // connect to MySQL

        $db=new MySQL(array
    ('host'=>'host','user'=>'user','password'=>'password','database'=>
    'database'));

        $result=$db->query('SELECT * FROM customers');

        // create 'ResultToString' processor object

        //$resultString=ResultProcessorFactory::createResultProcessor
    ('ResultToString',$result);

        header('Content-Type: text/plain');

        echo $resultString->fetch();

        /* displays the following string of data

       
    [id]=1
        [name]=Customer 1
    
        [email]=customer1@domain.com
    
       
    --------------------------
        [id]=2
        [name]=Customer 2
    
        [email]=customer2@domain.com
    
       
    --------------------------
        [id]=3
        [name]= Customer 3
    
        [email]=customer3@domain.com
    
       
    --------------------------
        [id]=4
        [name]= Customer 4
    
        [email]=customer4@domain.com
    
       
    --------------------------
        [id]=5
        [name]= Customer 5
    
        [email]=customer5@domain.com
    
       
    --------------------------

    */

    }

    catch(Exception $e){

        echo $e->getMessage();

        exit();

    }

    As you can see, the first example uses the static “createResultProcessor()·" method to display the string of data show above. Again, I’d like to stress that no class instance has been created, since the method has been called from outside the object context.

    Once you understand the previous example, take a look at the following one, which generates some XML nodes from the same result set:

     

    try{
    
    //connect to MySQL
    
    	$db=new MySQL(array('host'=>'host','user'=>'user',
    'password'=>'password','database'=>'database')); $result=$db->query('SELECT * FROM customers'); // create 'ResultToString' processor object //$resultString=ResultProcessorFactory::createResultProcessor
    ('ResultToString',$result); header('Content-Type:text/plain'); echo $resultString->fetch(); /* displays the following string of data
       
    [id]=1
        [name]=Customer 1
    
        [email]=customer1@domain.com
    
       
    --------------------------
        [id]=2
        [name]=Customer 2
    
        [email]=customer2@domain.com
    
       
    --------------------------
        [id]=3
        [name]= Customer 3
    
        [email]=customer3@domain.com
    
       
    --------------------------
        [id]=4
        [name]= Customer 4
    
        [email]=customer4@domain.com
    
       
    --------------------------
        [id]=5
        [name]= Customer 5
    
        [email]=customer5@domain.com
    
       
    --------------------------
    */ } catch(Exception $e){ echo $e->getMessage(); exit(); }

     

    Finally, here is the last example, which returns the same MySQL result set as an associative array:

     

    try{
        // connect to MySQL
        $db=new MySQL(array('host'=>'host','user'=>'user',
    'password'=>'password','database'=>'database')); $result=$db->query('SELECT * FROM customers'); $resultArray=ResultProcessorFactory::createResultProcessor
    ('ResultToArray',$result); print_r($resultArray->fetch()); } catch(Exception $e){ echo $e->getMessage(); exit(); } /* displays the following: Array ( [0]=> Array ( [id]=> 1 [name]=> Customer1
    [email]=> customer1@domain.com ) [1]=> Array ( [id]=> 2 [name]=>
    Customer 2 [email]=>customer2@domain.com) [2]=> Array ( [id]=> 3
    [name]=> Customer 3[email]=> customer3@domain.com ) [3]=> Array
    ( [id]=> 4 [name]=> Customer 4 [email]=> customer4@domain.com )
    [4]=> Array ( [id]=> 5 [name]=> Customer 5 [email]=>
    customer5@domain.com ) [5]) ) */

     

    Final thoughts

    In this two-part series, I walked you through the basics of using static members and methods with PHP 5. As you hopefully learned by the hands-on examples, if you need to call specific methods without having to deal with instances of a class, then static methods might be an option that you should take into account during the development of an application.

    As usual, see you in the next PHP tutorial!



     
     
    >>> 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