HomePHP Inheritance and Polymorphism in PHP: Building a Form Generator - Part I
Inheritance and Polymorphism in PHP: Building a Form Generator - Part I
Object Oriented Programming (OOP) is a powerful technology for designing Web applications. This article is the first in a three part series that will explain two of the bases of OOP, Inheritance and Polymorphism, and apply these principles to building an extensible form generator.
If you ever tasted the power of Object Oriented Programming (OOP) in PHP, then you probably know that it's an extremely useful way to manage medium and large projects, where code reusability, extendibility and maintainability are key factors that make developing Web applications a relatively painless experience. Without a doubt, with the release of PHP 5, these advantages have been greatly enhanced due to the highly improved object model (to name a few new excellent features), which turns the present and hopefully, the future of OOP in PHP bright and extremely promising.
However, when we're talking about PHP 4, often there are complaints about the lack of support when it comes to applying OOP. Many reasons form the foundation for this criticism, such as an absence of access modifiers, lack of abstract classes and even no support for multiple inheritance. While this is all true, in fact nothing stops developers from applying OOP theory in real applications. All that you need is the knowledge to implement OOP in a fairly decent way.
Although opinions may differ (and certainly it's out of the scope of this article), I strongly believe that the limitations present in PHP 4 are easily overcome, giving programmers a valid reason to keep using OOP in PHP programming.
Since we pushed some misconceptions out of the way, it's time to focus our attention on two primary bases of OOP: Inheritance and Polymorphism. In the rest of this article, we'll introduce these concepts as they are applied in PHP, with a rather practical approach, in order to find their place in a real application: building an extensible form generator.
For experienced programmers, this surely will look like a rather boring topic. However, for beginning and intermediate PHP users, this might be a straightforward way to gracefully enter the OOP arena, without feeling completely overwhelmed. Keep reading; you'll find it's quite worthwhile.