Home arrow PHP arrow 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.

  1. Polymorphs in PHP: Using Interfaces and Abstract Classes to Construct HTML Paragraphs
  2. Review: building a polymorph object using an abstract class and an interface
  3. Displaying HTML paragraphs using an object-oriented approach
  4. The polymorph class in action
By: Alejandro Gervasio
Rating: starstarstarstarstar / 2
January 19, 2010

print this article



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!

>>> More PHP Articles          >>> More By Alejandro Gervasio

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


- 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: