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

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


    Table of Contents:
  • Inheritance and Polymorphism in PHP: Building a Form Generator - Part II
  • That refreshing touch: core definition for base class and subclasses
  • There is a long list in your life: listing the full code for each subclass
  • Coming up: more subclasses
  • Implementing the Form Generator: take one

  • 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 II - That refreshing touch: core definition for base class and subclasses
    ( Page 2 of 5 )

    Since the subclasses can quite easy be entirely defined, as you'll see in a moment, before looking at their whole source code and logic it would be useful to refresh our memory of the skeletal structure for at least a couple of them. To begin with, let's show the code for the base class, as it was originally defined:

    class formObject {

    var $label;

    var $name;

    var $style;

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

    (!is_numeric($label))?$this->label=$label:die('Invalid
    parameter '.$label);

    (!is_numeric($name))?$this->name=$name:die('Invalid
    parameter '.$name);

    (!is_numeric($style))?$this->style=$style:die('Invalid
    parameter '.$style);

    }

    function generateHTML(){

    echo 'You are not overriding the generateHTML() method
    of the formObject super class!';

    }

    }

    The above base class is really acting as the core structure for defining any further form element. It offers three common properties ($label, $name and $style) shared by any form element, being inherited in the derived subclasses. It also offers the "generateHTML()" method, which, as we'll see shortly, will take care of generating the proper HTML for each type of form object (text inputs, radio buttons, and so forth).

    Because each subclass is rather defined in a similar fashion, let's remember the basic definitions for the input text and the radio button objects, just to make sure that we're heading the right way for building the form generator. The basic code for the subclass tied to a text input element looked like this:

    // 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(){

    }

    }

    And, for radio buttons, the code was the following:

    // 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(){

    }

    }

    We're not going to list the basic code for each subclass tied to a form element again! That was already done in the previous article. However, demonstrating how they looked in their original incarnation is the perfect foundation for moving forward and seeing the complete code for each subclass.

    If you ever have worked with HTML forms (and who hasn't?), then you know that they offer numerous elements. It seems that the list for all of the subclasses is rather lengthy. If you don't take my word for it, take a look at the following section.



     
     
    >>> 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 3 Hosted by Hostway
    Stay green...Green IT