PHP
  Home arrow PHP arrow Page 5 - Inheritance and Polymorphism in PHP: B...
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Sun Developer Network 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Mobile Linux 
App Generation ROI 
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: 5 stars5 stars5 stars5 stars5 stars / 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:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb 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.


    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

       · In this final article of the series, I expose the core concepts about Polymorphism,...
       · Hi,Thanks for the article, very cool. I am new to php and trying to learn sort...
       · Thanks for the comments about the article.In fact, integrating this set of classes...
       · This is a great series of articles! Two questions though:1) Is there any way you...
       · Hello Adams,Thank you for the comments about the article. They're...
       · Here´s the full source code for each...
       · Hello Alejandro. First, thanks very much for the many interesting tutorials. A...
       · Hello,Thanks for the compliments on my PHP articles, and I really appreciate...
       · Thanks very much Alejandro. It works.Now I need to go back and compare this with...
       · I'm glad to know that all the classes I listed here were useful to get the script...
       · Hi again Alejandro.I see what my problem was. I got stuck at the step with...
       · Hello again,I'm feeling glad of knowing that the problems has been solved. Also,...
       · Dear Alejandro,Thanks for the tutorials on php to which I'm new. Having cut and...
       · Hi Vince,Thank you for the kind comments on my PHP article. Now, concerning your...
     

       

    PHP ARTICLES

    - Working With Different Namespaces in PHP 5
    - User Management Explained: Overview
    - Using Namespaces in PHP 5
    - Database Security: Guarding Against SQL Inje...
    - Building a Modular Exception Class in PHP 5
    - Database and Password Security for Web Appli...
    - Handling MySQL Data Set Failures in PHP 5
    - Building Site Registration for Web Applicati...
    - Intercepting Customized Exceptions in PHP 5
    - Securing Your Web Application Against Attacks
    - Sub Classing Exceptions in PHP 5
    - Authentication for Web Application Security
    - Building a Content Management System with Co...
    - Filters and Login Systems for Web Applicatio...
    - Working with the Email Class in Code Igniter





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