Home arrow PHP arrow Page 3 - Abstract Classes in PHP: Setting Up a Concrete Example

Deriving subclasses from the parent abstract class: defining the “resultProcessor” class - PHP

Welcome to part two of the series “Abstract classes in PHP.” In three tutorials, this series introduces the key concepts of abstract classes in PHP 4-PHP 5, and explores their application and use in different object-oriented development environments. Whether you’re an experienced PHP developer wanting to fill in some gaps related to abstract classes, or only a beginner starting to taste the power of object-based programming in PHP, hopefully you’ll find this series enjoyable and instructive.

TABLE OF CONTENTS:
  1. Abstract Classes in PHP: Setting Up a Concrete Example
  2. Setting up a concrete example: Defining the hierarchy of classes
  3. Deriving subclasses from the parent abstract class: defining the “resultProcessor” class
  4. More subclasses ahead: defining the “fileProcessor” class
  5. Assembling the pieces: putting the classes to work together
By: Alejandro Gervasio
Rating: starstarstarstarstar / 15
February 01, 2006

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

As I stated before, the sample “dataProcessor” class is in fact a highly generic blueprint, seated on top of the class hierarchy; this comes in useful for creating subclasses that implement a specific functionality. Now that the overall features of any data processor object have been defined, we next need to derive a subclass that provides a concrete implementation for each of the empty methods that you saw previously.

Considering this situation, here’s the definition for the “resultProcessor” child class, which as you’ll see, exposes a concrete definition for each method inherited from its parent class. Take a look:

// derive 'resultProcessor' class from 'dataProcessor'
class resultProcessor extends dataProcessor{
 var $result;
 function resultProcessor($result){
  if(get_resource_type($result)!='mysql result'){
   trigger_error('Invalid MySQL result',E_USER_ERROR);
   exit();
  }
  $this->result=$result;
 }
 // returns database table data as string
 function toString(){
  $data='';
  while($row=mysql_fetch_row($this->result)){
   foreach($row as $col){
    $data.=$col;
   }
   $data.="\n";
  }
  return $data;
 }
 // returns database table data as array
 function toArray(){
  $data=array();
  while($row=mysql_fetch_row($this->result)){
   $data[]=$row;
  }
  return $data;
 }
 // returns database table data as XML
 function toXML(){
  $xml='<?xml version="1.0" encoding="iso-8859-1"?>'."\n";
  $xml.='<tabledata>'."\n";
  $data=$this->toArray();
  foreach($data as $row=>$value){
   foreach($value as $field){
    $xml.='<fieldnode>'.$field.'</fieldnode>'."\n";
   }
  }
  $xml.='</tabledata>';
  return $xml;
 }
}

With reference to the above child class, definitely there are many things worth noting here. Since this class only processes MySQL result sets, the constructor first checks to see whether its incoming parameter $result is really a MySQL result set. The checking code below illustrates this condition:

if(get_resource_type($result)!='mysql result'){
 trigger_error('Invalid MySQL result',E_USER_ERROR);
 exit();
}

After verifying that a real MySQL data set has been passed on as parameter, the class simply assigns it as the only property. Indeed, this is a short and efficient process.

Now, let’s move on and pay attention to the next class method, “toString()”, which takes the provided MySQL result set and returns it in a string format, where each row of the data set is delimited by a new line (\n) character:

function toString(){
       $data='';
 while($row=mysql_fetch_row($this->result)){
  foreach($row as $col){
   $data.=$col;
  }
  $data.="\n";
 }
 return $data;
}

You’ll remember from my schematic “dataProcessor” class, that each method for processing data wasn’t explicitly defined. However, in consonance with the definition of an abstract class, the derived “resultProcessor” class does expose a specific definition for each pertinent method. Now, do you see how child classes implement specifically all the generic methods declared within the base abstract class? I hope you do.

In a similar way, the remaining methods, “toArray()” and “toXML()” in question, expose concrete definitions, as you can appreciate from the snippet below:

function toArray(){
 $data=array();
 while($row=mysql_fetch_row($this->result)){
  $data[]=$row;
 }
 return $data;
}
// returns database table data as XML
function toXML(){
 $xml='<?xml version="1.0" encoding="iso-8859-1"?>'."\n";
 $xml.='<tabledata>'."\n";
 $data=$this->toArray();
 foreach($data as $row=>$value){
  foreach($value as $field){
   $xml.='<fieldnode>'.$field.'</fieldnode>'."\n";
  }
 }
 $xml.='</tabledata>';
 return $xml;
}

The subclass that I just created should clarify any questions regarding the appropriate implementation of abstract classes in PHP 4. After all, this isn’t a hard-to-grasp concept. However, to dissipate further doubts, let’s expand the previous example and derive another child class from the “dataProcessor” class, so you can see how the same generic methods you learned earlier are defined in a different way. Curious about how this is done? Right, keep on reading.



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