PHP
  Home arrow PHP arrow Page 4 - 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 I
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 35
    2005-04-05

    Table of Contents:
  • Inheritance and Polymorphism in PHP: Building a Form Generator - Part I
  • A Quick Look at Basic OOP Concepts
  • The Base Class of the Form Generator
  • Applying Inheritance: Creating Subclasses

  • 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 I - Applying Inheritance: Creating Subclasses


    (Page 4 of 4 )

    Having defined the generic base class for the form generator, it's fairly easy to derive any number of subclasses tied to each particular form element. After all, that's the big advantage of Inheritance. From this point onward, every defined subclass will inherit the $label, $name and $style properties, previously defined within the super class, as well as the "generateHTML()" method. Let's make Inheritance work for us and define the subclasses for building the form generator. Here are the initial definitions for the each form element's class:

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

    }

    }

    // class definition for hidden field object

    class hiddenFieldObject extends formObject {

    var $value;

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

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

    // properties setup

    }

    function generateHTML(){

    }

    }

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

    }

    }

    // class definition for checkbox object

    class checkBoxObject extends formObject {

    var $value;

    var $checked;

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

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

    // properties setup

    }

    function generateHTML(){

    }

    }

    // class definition for select box object

    class selectObject extends formObject {

    var $size;

    var $options;

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

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

    // properties setup

    }

    function generateHTML(){

    }

    }

    // class definition for textarea object

    class textAreaObject extends formObject {

    var $value;

    var $wrap;

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

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

    // properties setup

    }

    function generateHTML(){

    }

    }

    // class definition for button object

    class buttonObject extends formObject {

    var $value;

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

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

    // properties setup

    }

    function generateHTML(){

    }

    }

    // class definition for password object

    class passwordObject extends formObject {

    var $maxlength;

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

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

    // properties setup

    }

    function generateHTML(){

    }

    }

    // class definition for file object

    class fileObject extends formObject {

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

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

    // properties setup

    }

    function generateHTML(){

    }

    }

    // class definition for image object

    class imageObject extends formObject {

    var $value;

    var $src;

    var $alt;

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

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

    // properties setup

    }

    function generateHTML(){

    }

    }

    // class definition for submit object

    class submitObject extends formObject {

    var $value;

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

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

    // properties setup

    }

    function generateHTML(){

    }

    }

    Phew, I think that's the entire list for subclass definitions. In the above listing, we've implemented Inheritance by creating the proper subclasses that extend the original "formObject" base class, in order to generate the different form elements. Now, our form generator is beginning to be progressively modeled. While the subclasses have been defined with empty "generateHTML()" methods, as you can guess, the final version will include the logic for generating the HTML code for every specific element. For now we'll keep it pretty skeletal, which is handy for keeping the code easily readable.

    Summary

    And that's it for now. In this first part, we've been glancing at one of the big pillars of OOP in PHP, Inheritance. We've been using its capabilities for building an extensible form generator. Starting from a generic "formObject" base class, we've extended its definition to derive the needed subclasses.

    Hopefully this has been a practical approach to demonstrate the use of inherited methods and properties and their correct syntax. However, the best part is coming up. In the second part of this article series, we'll be exposing the complete source code for each subclass and explaining another core concept of OOP: Polymorphism. In the meantime, have some fun classing with PHP!


    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.

       · Eve the subject has been treated many times on books and articles, the article is...
       · That's a really awkward place to stop Part I. If you didn't have time to finish the...
       · Uh, please pardon the tone of my post above. You made it quite clear in the summary...
       · Your apologies are accepted. No problem about that. Even my schedule is pretty...
       · In trying to understand all that was said I am a little confused about some things...
       · Well I have some expirience programing php and I would like to suggest that in many...
       · Sorry, I forgot to include a sample of the “alternative syntax” I was referring to....
       · Yes, you're correct. Sometimes when you have many parameters to be passed to the...
       · Hi friend,I guess you're refering to the ternary operator. Well, it's an easy way...
       · Hi there,I guess you're talking about the ternary operator. It's often used to...
       · The usage of the :: syntax is the following. You can call a function defined in an...
       · Thanx for the great tutorial, I've used in some way OO programing in my scripts but...
       · Hi dear friend,I'm glad you found this tutorial useful for your growing...
       · Is there any reason why u decided to use PHP4 and its object syntax? Wouldn't it be...
       · Despite the fact that PHP5 offers much better support for OOP, their usage is still...
       · Thanks for sharing us your very nice article! jen
       · Thank you for your compliments about the article. Your comments are deeply...
     

       

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