PHP
  Home arrow PHP arrow Page 4 - 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? 
Google.com  
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 - More subclasses ahead: defining the “fileProcessor” class
    ( Page 4 of 5 )

    In order to keep the hierarchy of sample classes pretty balanced, I’m going to derive another child class from the parent “dataProcessor” class, so you can understand how the same generic methods can be implemented differently by this subclass. For this reason, I’ll define the “fileProcessor” class, whose signature is listed below:

    class fileProcessor extends dataProcessor{
     var $file;
     function fileProcessor($file){
      if(!file_exists($file)){
       trigger_error('Invalid file!',E_USER_ERROR);
       exit();
      }
      $this->file=$file;
     }
     // returns filedata as string with <br /> tags
     function toString(){
      return nl2br(file_get_contents($this->file));
     }
     // returns file data as array
     function toArray(){
      return file($this->file);
     }
     // returns file data as XML
     function toXML(){
      $xml='<?xml version="1.0" encoding="iso-8859-1"?>'."\n";
      $xml.='<filedata>'."\n";
      $filedata=$this->toArray();
      foreach($filedata as $row){
       $xml.='<filenode>'.trim($row).'</filenode>'."\n";
      }
      $xml.='</filedata>';
      return $xml;
     }
    }

    As you can see, here the above child class provides specific implementations for all the generic methods originally defined within the abstract class, which I find very useful for creating a simple file processor. Stripped down to its bare bones, this class accepts the name of the file for processing and uses each one of its respective methods for returning file data in different formats.

    Even when all class methods are very simple and understandable, it’s worth noting how they perform different tasks, according to the specific implementation within the two subclasses that I just created.

    Additionally, if you’re pretty familiar with the major pillars of object-oriented programming, then you’ll realize that both “resultProcessor” and “fileProcessor” classes will spawn polymorphic objects, since they use the same set of methods, but actually perform different operations. I'm sure you’ll agree with me that this is a very cool and powerful concept.

    So far, I’ve derived two subclasses from one base abstract class, something that hopefully has contributed to expanding your knowledge of using abstract classes in PHP 4. However, wouldn’t it be a nice corollary for this article to include all the sample classes in one script and spawn some objects? Right, let’s jump into the next section and see how these classes work together.



     
     
    >>> More PHP Articles          >>> More By Alejandro Gervasio
     

       

    PHP ARTICLES

    - Implementing Factory Methods in PHP 5
    - Merging a File Split for FTP Upload using PHP
    - Getting Data from Yahoo Site Explorer Inboun...
    - Method Chaining: Adding More Selecting Metho...
    - How to Split a File During an FTP Upload Usi...
    - Expanding a Custom CodeIgniter Library with ...
    - Using the Yahoo Site Explorer Inbound Links ...
    - Building a CodeIgniter Custom Library with M...
    - Building an E-mini Trading System Using PHP ...
    - Completing the MySQL Class with Method Chain...
    - 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





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 5 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek