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

Creating the ResultToString, ResultToXML and ResultToArray classes - 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 the title of this section indicates, my intention consists basically of building up three different classes, where each of them will be tasked with taking up a given MySQL result set. They will then apply to it the proper conversion process; that is, they will turn the data set rows into plain strings, XML data and an associative array respectively. Finally, they will return the results to calling code.

All right, after explaining the main purpose of each one of the classes that I plan to create, it’s time to see how they look, right? I have listed their source code below, so check them out:

// define 'ResultToString' class

 

class ResultToString{
private $result;
public function
__construct(Result $result){
      
$this->result=$result;
}
// fetch result set as plain text
public function fetch(){
$str='';
      
while($row=$this->result->fetchRow()){
foreach($row as $key=>$value){
        
$str.='['.$key.']='.$value."n";
}
         $str.='--------------------------'."n";
}
return $str;
}
}
// define 'ResultToXML' class
class ResultToXML{
private $result;
public function
__construct(Result $result){
       $this->result=$result;
}
// fetch result set as XML
public function fetch(){
$xml='<?xml
version="1.0"
encoding="iso-8859-1"?>'."n".'<data>'."n";

 

        while($row=$this->result->fetchRow()){        $xml.='<row>'."n"; foreach($row as $key=>$value){               $xml.='<'.$key.'>'.$value.'</'.$key.'>'."n"; }            $xml.='</row>'."n"; }         $xml.='</data>'."n"; return $xml; } } // define 'ResultToArray' class class ResultToArray{ private $result; public function __construct(Result $result){        $this->result=$result; } // fetch result set as array public function fetch(){ $data=array();       while($row=$this->result->fetchRow()){ $data[]=$row; } return $data; } }

 

As you’ll realize, the logic of the three classes shown above is fairly easy to understand. Essentially, each of them will accept an object of type “Result” inputted via the corresponding constructor. The object will be used from inside the class to convert a given MySQL result set to the corresponding format, that is plain strings, XML or an associative array.

With reference to the conversion process in question, this will be performed in all the previously defined classes by using their “fetch()” method, which obviously implements a different logic in accordance with the type of data that must be returned to calling code. Simple and efficient, isn’t it?

Okay, at this point I defined the pair of MySQL processing classes that you saw in a previous section, as well as the three that were created a few lines above and are handy for generating several outputs from a specific result set. However, until now I've not given you a single clue about how and why we will include static methods within the classes just defined. So, where will they be coded?

Fortunately, I have a good answer for that question. Since my primary objective is to generate disparate data formats from a single MySQL result set, the next step that I’m going to take will consist of creating an additional class, which will be capable of returning distinct data set processor objects  to produce the range of outputs that I mentioned before.

How will this be done? Yep, you guessed right. The entire process for returning result set processor objects to calling code will be handled by a static method! After all, we’re not far from reaching our goal, but this requires you to jump straight into the next section and continue reading. Thus, go ahead. I’ll be there, waiting for you.



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