PHP
  Home arrow PHP arrow Page 3 - Abstract Classes in PHP: Setting Up a Concrete Example
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? 
PHP

Abstract Classes in PHP: Setting Up a Concrete Example
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 15
    2006-02-01


    Table of Contents:
  • Abstract Classes in PHP: Setting Up a Concrete Example
  • Setting up a concrete example: Defining the hierarchy of classes
  • Deriving subclasses from the parent abstract class: defining the “resultProcessor” class
  • More subclasses ahead: defining the “fileProcessor” class
  • Assembling the pieces: putting the classes to work together

  • 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


    Abstract Classes in PHP: Setting Up a Concrete Example - Deriving subclasses from the parent abstract class: defining the “resultProcessor” class
    ( Page 3 of 5 )

    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
     

       

    PHP ARTICLES

    - 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
    - Method Chaining: Adding More Methods to the ...
    - Method Chaining in PHP 5
    - The Role of Interfaces in Applying the Depen...
    - Dependency Injection: Using a Setter Method ...
    - Using a Model Class with the Dependency Inje...
    - Injecting Objects Using Setter Methods with ...
    - Injecting Objects by Constructor with the De...
    - The Dependency Injection Design Pattern in P...
    - Performing Inferential Statistical Analysis ...
    - Performing Descriptive Statistical Analysis ...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 4 Hosted by Hostway
    Stay green...Green IT