PHP
  Home arrow PHP arrow Page 4 - Generating Outputs from MySQL with Sta...
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Sun Developer Network 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Mobile Linux 
App Generation ROI 
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? 
PHP

Generating Outputs from MySQL with Static Members and Methods in PHP 5
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 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:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb 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!


    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

       · This final article of the series is pointed out to demonstrate how to use static...
     

       

    PHP ARTICLES

    - Working With Different Namespaces in PHP 5
    - User Management Explained: Overview
    - Using Namespaces in PHP 5
    - Database Security: Guarding Against SQL Inje...
    - Building a Modular Exception Class in PHP 5
    - Database and Password Security for Web Appli...
    - Handling MySQL Data Set Failures in PHP 5
    - Building Site Registration for Web Applicati...
    - Intercepting Customized Exceptions in PHP 5
    - Securing Your Web Application Against Attacks
    - Sub Classing Exceptions in PHP 5
    - Authentication for Web Application Security
    - Building a Content Management System with Co...
    - Filters and Login Systems for Web Applicatio...
    - Working with the Email Class in Code Igniter





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway
    Stay green...Green IT