PHP
  Home arrow PHP arrow Page 3 - 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 - Polymorphism: the core definition


    (Page 3 of 5 )

    Without a doubt, Polymorphism is extremely useful in strongly typed languages such as Java or C++. But in a weakly typed language such as PHP, where the interpreter doesn't care about the types of the objects manipulated, this technique is not going to have a huge impact when used.

    However, taking a practical approach, the concept may have more sense. But, first, what is Polymorphism? Well, basically it means that different classes can have different behaviors for the same operation. In other words, a single class, method or variable may present itself in many different forms.

    Certainly this is very interesting, but if we think about it, because of PHP's nature, this is happening automatically most of the time, since there is not a native concept of strong typing. Does this sound a bit confusing? To demonstrate a practical use, let's work with the example listed in the first part of this series, which defines a simple class to display a message:

    class basicMessage {

    var $message;

    function basicMessage($message){

    $this->message=$message;

    }

    function displayMessage(){

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

    }

    }

    The base class has a single $message property, which is directly displayed in the browser using the "displayMessage()" method. So far, this is fairly understandable. Now, let's define a couple of subclasses derived from the base class, in the following manner:

    class derivedMessageOne extends basicMessage {

    function derivedMessageOne($message){

    parent::basicMessage($message);

    }

    //override base class displayMessage() method

    function displayMessage(){

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

    }

    }

    class derivedMessageTwo extends basicMessage {

    function derivedMessageTwo($message){

    parent::basicMessage($message);

    }

    //override base class displayMessage() method

    function displayMessage(){

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

    }

    }

    Once we've created the two subclasses, let's instantiate some objects to see Polymorphism in action:

    $messages=array($basicMessage=&new basicMessage('Hello'),$derivedMessageOne=&new derivedMessageOne('Hello'),$derivedMessageTwo=&new derivedMessageTwo('Hello'));

    foreach($messages as $message){

    $message->displayMessage();

    }

    The output for the above snippet would look like this:

    Hello from base class
    Hello from derivedMessageOne subclass
    Hello from derivedMessageTwo subclass

    We simply instantiated an object from the base class $basicMessage, and two objects from the derived subclasses, $derivedMessageOne and $derivedMessageTwo, respectively. Then, we created an array with the objects, iterating over them and calling in turn its "displayMessage()" method. As you can see, it's nothing special. However, there is something really interesting happening here. Did you notice that each time the "displayMessage()" is invoked inside the loop, the output is different. Isn't this funny?

    Here's where Polymorphism comes in. We can say that the subclasses "derivedMessageOne" and "derivedMessageTwo" are derived from the base class, establishing a "is a" relationship with "basicMessage", but they behave differently when their "displayMessage()" method is called. Thus, we have a class, which is behaving differently for the same method call. So, you're probably wondering: where's the big deal in that?

    Maybe this is not going to change your big programming plans, but the concept has an inherent application. What if we define a base class to encapsulate generic code, and then, according to our needs, derive any number of subclasses, which expose specific properties and methods? It's definitely an efficient and elegant approach to implementing some applications.

    At this point, with a little bit of theory behind us, we have a good approach for implementing our form generator, turning it in a true polimorphic object. So, let's present the new version of the form generator.

    More PHP Articles
    More By Alejandro Gervasio


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