Home arrow PHP arrow Page 4 - Generating Outputs from MySQL with Static Members and Methods in PHP 5

Defining the ResultProcessorFactory class - PHP

Trying to expand beyond the boundaries of your existing background by learning how to code and use static methods and properties inside your PHP 5 classes? Then look no further. Welcome to the last part of the series “Using static members and methods in PHP 5.” Made up of two instructive chapters, this series introduces the foundations of using static members and defines static methods in PHP 5-driven development environments.

TABLE OF CONTENTS:
  1. Generating Outputs from MySQL with Static Members and Methods in PHP 5
  2. Working with MySQL
  3. Creating the ResultToString, ResultToXML and ResultToArray classes
  4. Defining the ResultProcessorFactory class
By: Alejandro Gervasio
Rating: starstarstarstarstar / 4
October 09, 2006

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

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
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PHP ARTICLES

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