Home arrow PHP arrow Page 3 - Inheritance and Polymorphism in PHP: Building a Form Generator - Part III

Polymorphism: the core definition - PHP

In the last part of this three-part series of articles, we will find out how to improve the form generator we created in part II by learning about Polymorphism and adding it to the mix.

TABLE OF CONTENTS:
  1. Inheritance and Polymorphism in PHP: Building a Form Generator - Part III
  2. Turning back time: that old inefficient script
  3. Polymorphism: the core definition
  4. Implementing the Form Generator: take two
  5. Listing full source code
By: Alejandro Gervasio
Rating: starstarstarstarstar / 18
April 19, 2005

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

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
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PHP ARTICLES

- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: