Home arrow PHP arrow Page 3 - Polymorphs in PHP: Using Interfaces and Abstract Classes to Construct HTML Paragraphs

Displaying HTML paragraphs using an object-oriented approach - PHP

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 I explained in the preceding section, to demonstrate that it’s possible to construct polymorph objects by combining the functionality of interfaces and abstract class, I"m going to derive a new child class from the parent “HtmlElement,” which also will be an implementer of the “Parser” interface.

Here’s the definition of the new subclass, whose function is to render HTML paragraphs:

// define Paragraph class (subclass of HtmlElement class)

class Paragraph extends HtmlElement


public function parseContent()


if ($this->content !== '')


$this->content = str_replace(array('</p>','</p>)'), "", $this->content);



return $this;


// render paragraph element

public function render()


return '<p id="' . $this->id . '" class="' . $this->class . '">' . $this->content . '</p>';




Didn’t I tell you that building a polymorph class by using interfaces and abstract classes was a straightforward process? Well, if you pay close attention to the above “Paragraph” class, then you’ll realize that I was right.

Obviously, aside from inheriting all of the methods defined by its corresponding parent, this child class gives concrete implementation to the “parseContent()” method declared within the “Parser” interface. Nonetheless, this time the method will remove any “<p></p>” tags included in the contents of the HTML paragraphs being constructed.

Do you see how simple it is to make two classes that belong to the same family and implement the same interface behave entirely differently? I guess you do. However, to dissipate any doubts that you might have on this topic, in the last segment of this tutorial I’m going to set up an example that will show how to work with the previous “Paragraph” class.

Therefore, to see how this final example will be developed, read the following section. I’ll be there, waiting for you.

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