HomePHP Polymorphs in PHP: Using Interfaces and Abstract Classes to Construct HTML Paragraphs
Polymorphs in PHP: Using Interfaces and Abstract Classes to Construct HTML Paragraphs
In this penultimate part of a seven-part series on building polymorphs in PHP 5, I demonstrate how easy it is to build polymorph objects that merge the functionality of abstract classes and the structure of interfaces. The entire creation process is simple enough that you shouldn’t have major problems grasping its underlying logic.
As you may know, most of the mature programming languages that are used nowadays permit developers to implement Polymorphism in one form or another. Undoubtedly C, C++ and Java, for example, are languages that allow you to build polymorph structures, either at function-level or class-level. This characteristic is one of many that make them so powerful.
On the other hand, while PHP doesn’t offer the maturity of big players like Java or C++, and is mainly used for developing web applications rather than creating desktop-based programs, its object model lets developers achieve Polymorphism by using classes and interfaces. This can be very useful for building well-structured object-oriented applications.
However, there’s the possibility that you, as a passionate PHP programmer, are wondering if learning the fundamentals of Polymorphism is really worthwhile. Well, the answer is a resounding yes! By grasping its key concepts you’ll arm yourself with a solid background that will sharpen your existing programming skills.
Of course, if you already had the chance to read the previous part of this series, then you're probably familiar with building polymorph objects by combining the power of abstract classes and interfaces. In that tutorial I explained how to create a polymorph class that inherited functionality from a base parent, and structure from a simple interface.
This class was responsible for rendering HTML divs through a method called “render().” Even though I discussed in depth how this class worked, this process is still incomplete. It's necessary to build another sample class that behaves differently when its “parseContent()” method gets called.
Thus, in this sixth part of the series I’m going to build a second polymorph class. It will be tasked with displaying HTML paragraphs by using the interface and the abstract class mentioned above.
Now, it’s time to start learning how to build another polymorph class in PHP 5. Let’s go!