PHP
  Home arrow PHP arrow Page 4 - Inheritance and Polymorphism in PHP: Building a Form Generator - Part I
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

Inheritance and Polymorphism in PHP: Building a Form Generator - Part I
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 40
    2005-04-05


    Table of Contents:
  • Inheritance and Polymorphism in PHP: Building a Form Generator - Part I
  • A Quick Look at Basic OOP Concepts
  • The Base Class of the Form Generator
  • Applying Inheritance: Creating Subclasses

  • 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


    Inheritance and Polymorphism in PHP: Building a Form Generator - Part I - Applying Inheritance: Creating Subclasses
    ( Page 4 of 4 )

    Having defined the generic base class for the form generator, it's fairly easy to derive any number of subclasses tied to each particular form element. After all, that's the big advantage of Inheritance. From this point onward, every defined subclass will inherit the $label, $name and $style properties, previously defined within the super class, as well as the "generateHTML()" method. Let's make Inheritance work for us and define the subclasses for building the form generator. Here are the initial definitions for the each form element's class:

    // class definition for input text object

    class inputTextObject extends formObject {

    var $value;

    var $maxlength;

    function inputTextObject($label,$name,$style,$value,$maxlength){

    parent::formObject($label,$name,$style);

    // properties setup

    }

    function generateHTML(){

    }

    }

    // class definition for hidden field object

    class hiddenFieldObject extends formObject {

    var $value;

    function hiddenFieldObject($label,$name,$style,$value){

    parent::formObject($label,$name,$style);

    // properties setup

    }

    function generateHTML(){

    }

    }

    // class definition for radio button object

    class radioButtonObject extends formObject {

    var $value;

    var $checked;

    function radioButtonObject($label,$name,$style,$value,$checked=''){

    parent::formObject($label,$name,$style);

    // properties setup

    }

    function generateHTML(){

    }

    }

    // class definition for checkbox object

    class checkBoxObject extends formObject {

    var $value;

    var $checked;

    function checkBoxObject($label,$name,$style,$value,$checked=''){

    parent::formObject($label,$name,$style);

    // properties setup

    }

    function generateHTML(){

    }

    }

    // class definition for select box object

    class selectObject extends formObject {

    var $size;

    var $options;

    function selectObject($label,$name,$style,$size,$options=array()){

    parent::formObject($label,$name,$style);

    // properties setup

    }

    function generateHTML(){

    }

    }

    // class definition for textarea object

    class textAreaObject extends formObject {

    var $value;

    var $wrap;

    function textAreaObject($label,$name,$style,$value,$wrap='virtual'){

    parent::formObject($label,$name,$style);

    // properties setup

    }

    function generateHTML(){

    }

    }

    // class definition for button object

    class buttonObject extends formObject {

    var $value;

    function buttonObject($label,$name,$style,$value){

    parent::formObject($label,$name,$style);

    // properties setup

    }

    function generateHTML(){

    }

    }

    // class definition for password object

    class passwordObject extends formObject {

    var $maxlength;

    function passwordObject($label,$name,$style,$maxlength){

    parent::formObject($label,$name,$style);

    // properties setup

    }

    function generateHTML(){

    }

    }

    // class definition for file object

    class fileObject extends formObject {

    function fileObject($label,$name,$style){

    parent::formObject($label,$name,$style);

    // properties setup

    }

    function generateHTML(){

    }

    }

    // class definition for image object

    class imageObject extends formObject {

    var $value;

    var $src;

    var $alt;

    function imageObject($label,$name,$style,$value,$src,$alt){

    parent::formObject($label,$name,$style);

    // properties setup

    }

    function generateHTML(){

    }

    }

    // class definition for submit object

    class submitObject extends formObject {

    var $value;

    function submitObject($label,$name,$style,$value){

    parent::formObject($label,$name,$style);

    // properties setup

    }

    function generateHTML(){

    }

    }

    Phew, I think that's the entire list for subclass definitions. In the above listing, we've implemented Inheritance by creating the proper subclasses that extend the original "formObject" base class, in order to generate the different form elements. Now, our form generator is beginning to be progressively modeled. While the subclasses have been defined with empty "generateHTML()" methods, as you can guess, the final version will include the logic for generating the HTML code for every specific element. For now we'll keep it pretty skeletal, which is handy for keeping the code easily readable.

    Summary

    And that's it for now. In this first part, we've been glancing at one of the big pillars of OOP in PHP, Inheritance. We've been using its capabilities for building an extensible form generator. Starting from a generic "formObject" base class, we've extended its definition to derive the needed subclasses.

    Hopefully this has been a practical approach to demonstrate the use of inherited methods and properties and their correct syntax. However, the best part is coming up. In the second part of this article series, we'll be exposing the complete source code for each subclass and explaining another core concept of OOP: Polymorphism. In the meantime, have some fun classing with PHP!



     
     
    >>> 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 6 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek