PHP
  Home arrow PHP arrow Page 3 - 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 - There is a long list in your life: listing the full code for each subclass
    ( Page 3 of 5 )

    Let's get straight to the point. First, let's look at the subclass for input text objects. Here is its definition:

    class inputTextObject extends formObject {

    var $value;

    var $maxlength;

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

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

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

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

    }

    function generateHTML(){

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

    return $html;

    }

    }

    As you can deduce from the above listed code, the subclass extends the base class, adding two extra properties: $value and $maxlength. The constructor accepts the base class parameters, plus the additional ones, and calls the base class constructor for setting up the $label, $name and $style properties. That's very simple to understand. Also notice that the subclass exposes its own "generateHTML()" method, which generates the corresponding HTML for an text input element, according to its properties.

    Now, I think that you have an approximate idea of how the rest of the subclasses will look. And certainly you're correct. They're similarly defined, with a few different properties, according to the type of form element treated, but all of them offer the "generateHTML()" method. So, here's the definition for a hidden field subclass:

    class hiddenFieldObject extends formObject {

    var $value;

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

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

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

    }

    function generateHTML(){

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

    return $html;

    }

    }

    In as similar way, we can define the subclass for a radio button element:

    class radioButtonObject extends formObject {

    var $value;

    var $checked;

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

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

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

    ($checked=='checked'||$checked=='')?$this-
    >checked=$checked:die('Invalid parameter '.$checked);

    }

    function generateHTML(){

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

    ($this->checked=='checked')?$html.=' checked="'.$this-
    >checked.'" />'.$this->label:$html.=' />'.$this->label;

    return $html;

    }

    }

    If building checkboxes is your favorite hobby (mine is building submit buttons), here is the checkbox definition:

    class checkBoxObject extends formObject {

    var $value;

    var $checked;

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

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

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

    ($checked=='checked'||$checked=='')?$this-
    >checked=$checked:die('Invalid parameter '.$checked);

    }

    function generateHTML(){

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

    ($this->checked=='checked')?$html.=' checked="'.$this-
    >checked.'" />'.$this->label:$html.=' />'.$this->label;

    return $html;

    }

    }

    There are several subclasses to be defined. Don't forget them. Here are the select boxes:

    class selectObject extends formObject {

    var $size;

    var $options;

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

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

    (is_integer($size)&&$size>0)?$this->size=$size:die
    ('Invalid parameter '.$size);

    (count($options)>0)?$this->options=$options:die
    ('Invalid parameter '.$options);

    }

    function generateHTML(){

    $html=$this->label.'<select name="'.$this->name.'"
    class="'.$this->style.'" size="'.$this->size.'">';

    foreach($this->options as $option=>$value){

    $html.='<option value="'.$value.'">'.$option.'</option>';

    }

    $html.='</select>';

    return $html;

    }

    }

    At this point, half of the job is done. Let's catch our breath, then continue defining the rest of the subclasses.



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

       

    PHP ARTICLES

    - Using Directory Iterators to Build Loader Ap...
    - Using the spl_autoload() Functions to Build ...
    - Working Out of the Object Context to Build L...
    - Using the _autoload() Magic Function to Buil...
    - The Destruct Magic Function in PHP 5
    - The Autoload Magic Function in PHP 5
    - Developing a Recursive Loading Class for Loa...
    - The Sleep and Wakeup Magic Functions in PHP 5
    - Using the Clone Magic Function in PHP 5
    - Including Files Recursively with Loader Appl...
    - The Call Magic Function in PHP 5
    - Designing a Captcha System with PHP and MySQL
    - Using Static Methods to Build Loader Apps in...
    - The Isset and Unset Magic Functions in PHP 5
    - Advanced PHP Form Input Validation to Check ...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 2 hosted by Hostway
    Stay green...Green IT