HomePHP Inheritance and Polymorphism in PHP: Building a Form Generator - Part III
Inheritance and Polymorphism in PHP: Building a Form Generator - Part III
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.
Welcome to Part III of the series "Inheritance and Polymorphism in PHP: Building a Form Generator." I hope that you had read the previous articles, Part I and Part II, so you have a good understanding of the overall concepts explained during the development of these sections.
If you haven't read the previous articles, let's take a quick look at what we've done for now. In order to try out some of the most powerful foundations of OOP (Object Oriented Programming), we've faced the task of applying Inheritance and Polymorphism in PHP, by taking a rather practical approach and using these pillars to create a specific project: an extensible Form Generator.
To begin with, we defined a base class named "formObject", which is the super class from which we derived all of the necessary subclasses to generate each form element, using the power of Inheritance in PHP. Once we created the proper subclasses, all we needed to do to create our working Form Generator is instantiate some objects from the subclasses -- and that's it, we have an extensible form generator!
Well, not so fast. If we remind ourselves of the approach taken in the previous article, we'll have to admit that using a "switch" statement to determine what type of form object we're dealing with, is extremely inefficient. Our goal is to try to fix up that method and implement a much better solution. So, what's our next route to presenting a decent approximation? The answer is Polymorphism. Sounds like another buzz word, but it's really good to learn how it can be implemented in our Web projects.