PHP
  Home arrow PHP arrow Page 2 - Inheritance and Polymorphism in PHP: Building a Form Generator - Part I
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? 
Google.com  
PHP

Inheritance and Polymorphism in PHP: Building a Form Generator - Part I
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 40
    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:
      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 I - A Quick Look at Basic OOP Concepts
    ( Page 2 of 4 )

     

    Before we go more deeply into explaining the concept of Inheritance, for those new to OOP, let's define the concept of object itself. In the context of OO software, an object can be almost any item or concept, either a physical object (i.e. customers, users, and so forth), or conceptual objects that only exist within software, such as a text input field or a radio button.

    Object-oriented software is designed and built as a set of self-contained objects, with both attributes and operations (functions) that interact to make applications work properly. In OOP parlance, attributes are Properties or variables related to the object. Operations or functions are Methods that belong to the object to perform actions for modifying itself or for some other external effect.

    One of the major pillars of OOP is Encapsulation (also known as data hiding). In simple terms, access to the data within an object must be available via the object's methods, or the interface of the object. Modifiers and accessor methods should provide a convenient way to access and modify objects properties.

    Last, but not least, we need to explain Inheritance. We can create a hierarchical relationship between classes and derived subclasses. A subclass inherits properties and methods from its super class, a fact  we can use to save ourselves some work. If we write a super class that owns properties and methods, and then create subclasses that use the capabilities of the super class, we write it only once, and this is definitely better than writing those methods and properties on each subclass (many times, in other words).

    If a sentence about two classes makes sense with "is a" between the classes, from bottom to top, inheritance can probably be implemented appropriately. Consider that if we have an input text element, it might be thought of as "is a form element." So, if we would have a form element class, then the input text element can inherit from the form element.

    For clarifying the concept of Inheritance, let's show a simple and quick example, since the topic has been thoroughly covered in many books and articles. First, let's define a base class "basicMessage" for, not surprisingly, displaying messages:

    class basicMessage {

    var $message;

    function basicMessage($message){

    $this->message=$message;

    }

    function displayMessage(){

    echo 'Displaying '.$this->message.' from base class<br />'."\n";

    }

    }

     

    Now, let's instantiate an object from the base class:

     

    $msg=&new basicMessage('Hello, I am the Mom');

    $msg->displayMessage();

     

    The output for the above code is the following:

    Displaying Hello, I am the Mom from base class

    That's very simple and straightforward, right? Now, in order to implement Inheritance, let's define a subclass "boldedMessage," which inherits the properties and methods from "basicMessage:"

    class boldedMessage extends basicMessage {

    function boldedMessage($message){

    parent::basicMessage($message);

    }

    }

     

    Please notice that inside the "boldedMessage" constructor, we've called the constructor for "basicMessage," with the following syntax:

    parent::basicMessage($message);

    Now, if we instantiate an object from the subclass "boldedMessage," in the following manner:

    $childmsg=&new boldedMessage('Hello, I am the child');

    $childmsg->displayMessage();

    The visual output is as follows:

    Displaying Hello, I am the child from base class

    Certainly, the subclass is happily inheriting the $message property and the "displayMessage()" method from the base class. Also, for proper implementation, the base class definition must be available, for deriving any number of subclasses. Please don't forget that!

    If we ever change our mind, and decide to add the own "displayMessage()" method to the subclass "boldedMessage," overriding the original base class method, the class definition would look like this:

    class boldedMessage extends basicMessage {

    function boldedMessage($message){

    parent::basicMessage($message);

    }

    //override base class displayMessage() method

    function displayMessage(){

    echo '<strong>Displaying '.$this->message.' from subclass</strong><br />'."\n";

    }

    }

    By this point, the subclass is overriding the "displayMessage()," exposing its own method definition, in this case displaying a bolded message. Just by instantiating a new object from the redefined class, in a similar way to the previous examples:

    $childmsg=&new boldedMessage('Hello, I am the child');

    $childmsg->displayMessage();

    the output would be a bolded string message:

    Displaying Hello, I am the child from subclass 

    As you can see, any number of derived subclasses from the base class will inherit its properties and methods. Indeed, this is extremely useful in OOP. Just think about it. If we have a super class containing the core properties and methods, and for instance, we need to add another method to it, the new method will be automatically inherited from all of the defined subclasses. If you think that's not enough power, well you should run for president!

    Now let's take a more practical approach and use OOP in PHP to build something useful for Web applications. Did you read this article's title? Okay, we're going to develop an extensible form generator, using the goodies of Inheritance and Polymorphism. Keep reading.



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

       

    PHP ARTICLES

    - Implementing Factory Methods in PHP 5
    - Merging a File Split for FTP Upload using PHP
    - Getting Data from Yahoo Site Explorer Inboun...
    - Method Chaining: Adding More Selecting Metho...
    - How to Split a File During an FTP Upload Usi...
    - Expanding a Custom CodeIgniter Library with ...
    - Using the Yahoo Site Explorer Inbound Links ...
    - Building a CodeIgniter Custom Library with M...
    - Building an E-mini Trading System Using PHP ...
    - Completing the MySQL Class with Method Chain...
    - 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





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 2 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek