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.
Next: The Base Class of the Form Generator >>
More PHP Articles
More By Alejandro Gervasio
|
| · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | | |
|