PHP
  Home arrow PHP arrow Page 5 - Inheritance and Polymorphism in PHP: Building a Form Generator - Part III
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 III
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 14
    2005-04-19


    Table of Contents:
  • Inheritance and Polymorphism in PHP: Building a Form Generator - Part III
  • Turning back time: that old inefficient script
  • Polymorphism: the core definition
  • Implementing the Form Generator: take two
  • Listing full source code

  • 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 III - Listing full source code
    ( Page 5 of 5 )

    As stated previously, here's the full code for the form generator:

    // base class formObject

    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!';

    }

    }

    // derived class inputTextObject

    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;

    }

    }

    // derived class hiddenFieldObject

    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;

    }

    }

    // derived class radioButtonObject

    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;

    }

    }

    // derived class checkBoxObject

    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;

    }

    }

    // derived class selectObject

    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;

    }

    }

    // derived class textAreaObject

    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;

    }

    }

    // derived class buttonObject

    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;

    }

    }

    // derived class passwordObject

    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;

    }

    }

    // derived class fileObject

    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;

    }

    }

    // derived class imageObject

    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;

    }

    }

    // derived class resetObject

    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;

    }

    }

    // derived class submitObject

    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;

    }

    }

    Now you have some code to play around with. I hope you have a good time finding possible additions. Just be creative and put your background to work.

    Conclusion

    Over this three-part series, we've learned a bit more about the capabilities of Inheritance and Polymorphism in PHP 4, building a practical example to generate HTML forms using an Object-Oriented approach. Also, the process has introduced some theory concepts that hopefully will help you gain a better understanding of OOP. Without a doubt, the OOP approach is the best way to manage large projects in PHP, due to the pluggable nature of objects. When you have a couple of classes working for you, it's just a matter of making them interact to rapidly develop Web applications. The learning curve is rather long, but the effort is truly worthwhile.



     
     
    >>> 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 4 hosted by Hostway
    Stay green...Green IT