PHP
  Home arrow PHP arrow Page 2 - 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 - Setting up a concrete example: Defining the hierarchy of classes
    ( Page 2 of 5 )

    To illustrate how you can implement abstract classes in a concrete situation, what I’ll do next is set up an example, which will create a well-structured hierarchy of classes aimed at processing data coming from distinct sources. On top of this hierarchy, I’ll define an abstract, highly generic data processing class, which for obvious reasons won’t be able to be instantiated. Additionally, this class will expose three generic methods for converting data to different formats, but won’t provide explicit definitions for each of them (known as abstract methods).

    In accordance with the concepts deployed in the first tutorial of this series, I’ll derive two subclasses from the base abstract class, by providing a concrete definition for all the generic methods inherited from the corresponding parent class. Sounds confusing? It’s not at all. Trust me. I’ll begin by defining the base abstract class “dataProcessor()”, which looks like this:

    // define abstract class ‘dataProcessor’
    class dataProcessor{
            function dataProcessor(){
                    if(get_class($this)=='dataProcessor'||!
    is_subclass_of($this)){
                        trigger_error('This class is abstract.It
    cannot be instantiated!',E_USER_ERROR);
                    }
            }
            // define generic methods
            function toString(){}
            function toArray(){}
            function toXML(){}
    }

    As the above example shows, the abstract “dataProcessor” class has three empty methods, “toString()”, “toArray()” and “toXML()” respectively, aside from the corresponding constructor. Meeting the requirements of abstract classes, these methods don’t expose a specific definition themselves, because the base class only outlines the properties and methods of generic “dataProcessor” objects. Any further definition of these methods will be specifically provided within the pertinent child classes, which will implement a determined behavior in accordance with the functionality expected of each derived subclass.

    Once the abstract “dataProcessor” class has been defined, it’s easy to see that each subclass will expose a concrete definition for the “toString()”, “toArray()” and “toXML()” methods. Since this process will be understood best by example, let’s move on together and derive the first child class, which I’ve called “resultProcessor”, responsible for processing MySQL result sets.



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

       

    PHP ARTICLES

    - 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
    - Method Chaining: Adding More Methods to the ...





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