PHP
  Home arrow PHP arrow Page 4 - 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 - Coming up: more subclasses
    ( Page 4 of 5 )

    If you think that the above class definitions are not enough for building our form generator, you're right. There are still more form elements to be defined. So, it's time to continue defining more form subclasses. The next one to be defined is a <textarea> element, listed below:

    class textAreaObject extends formObject {

    var $value;

    var $wrap;

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

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

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

    ($wrap=='off'||$wrap=='physical'||$wrap=='virtual')?
    $this->wrap=$wrap:die('Invalid parameter '.$wrap);

    }

    function generateHTML(){

    $html=$this->label.'<textarea name="'.$this->name.'"
    class="'.$this->style.'" wrap="'.$this-
    >wrap.'">'.$this->value.'</textarea>';

    return $html;

    }

    }

    The above listed class for building a <textarea> element accepts two additional parameters: $value and $wrap, in order to use them for proper HTML generation. In this case, we've given a default value of "virtual" for the $wrap parameter.

    Now let's look at the form button elements. Its class definition is the following:

    class buttonObject extends formObject {

    var $value;

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

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

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

    }

    function generateHTML(){

    $html=$this->label.'<input type="button" name="'.$this-
    >name.'" value="'.$this->value.'" class="'.$this-
    >style.'" />';

    return $html;

    }

    }

    Our next step consists of listing the class for generating password fields. Here's the code:

    class passwordObject extends formObject {

    var $maxlength;

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

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

    (is_int($maxlength)&&$maxlength>8&&$maxlenght<24)?
    $this->maxlength=$maxlength:die('Invalid parameter
    '.$maxlength);

    }

    function generateHTML(){

    $html=$this->label.'<input type="password"
    name="'.$this->name.'" value="'.$this->value.'"
    class="'.$this->style.'" />';

    return $html;

    }

    }

    Okay, let's put in a bit more effort, and define the class for file uploads elements:

    class fileObject extends formObject {

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

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

    }

    function generateHTML(){

    $html=$this->label.'<input type="file" name="'.$this-
    >name.'" class="'.$this->style.'" />';

    return $html;

    }

    }

    Let's not forget to define the subclass for reset buttons. Here is the code:

    class resetObject extends formObject {

    var $value;

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

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

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

    }

    function generateHTML(){

    $html=$this->label.'<input type="reset" name="'.$this-
    >name.'" value="'.$this->value.'" class="'.$this-
    >style.'" />';

    return $html;

    }

    }

    Finally, we're going to show our last two classes: form image elements and submit buttons. First, here's the code for form image elements, often used for submitting forms without using the venerable submit button:

    class imageObject extends formObject {

    var $value;

    var $src;

    var $alt;

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

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

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

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

    (file_exists($src))?$this->src=$src:die('Invalid
    parameter '.$src);

    }

    function generateHTML(){

    $html=$this->label.'<input type="image" name="'.$this-
    >name.'" value="'.$this->value.'" class="'.$this-
    >style.'" src="'.$this->src.'" alt="'.$this-
    >alt.'" />';

    return $html;

    }

    }

    Finally, the class definition for a submit button:

    class submitObject extends formObject {

    var $value;

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

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

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

    }

    function generateHTML(){

    $html=$this->label.'<input type="submit" name="'.$this-
    >name.'" value="'.$this->value.'" class="'.$this-
    >style.'" />';

    return $html;

    }

    }

    I must give a short disclaimer: notice that in all of the subclasses that accept a $value parameter, we've opted not to consider as a valid argument any numeric value. While this validation process could be leaving out some potentially valid parameters, we've chosen to do this based on a data type checking rather than performing validation on data length. For particular cases, just implement the validation process you believe best suits your needs.

    Now we have fully defined each subclass derived from the form base class for creating the form generator. Since subclasses nicely expose the "generateHTML()" method, it is easy to figure out a technique to put all of the subclasses to work and build a complete HTML form. So, let's try creating a regular form, using the above defined classes.



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