Home arrow PHP arrow Page 4 - Combining Interfaces and Abstract Classes to Build Polymorphs in PHP

Building an improved polymorph class - PHP

In this fifth part of a seven-part series on building polymorphs in PHP 5, you will learn how to build a polymorph object by combining an abstract class and a simple interface. As you'll see, this object will be used for constructing basic HTML divs.

  1. Combining Interfaces and Abstract Classes to Build Polymorphs in PHP
  2. Achieving Polymorphism via abstract classes
  3. Defining a simple interface and an abstract class
  4. Building an improved polymorph class
By: Alejandro Gervasio
Rating: starstarstarstarstar / 1
January 14, 2010

print this article



In the earlier section I defined a simple interface in conjunction with the “HtmlElement” abstract class, so next I'll build a subclass that implements the interface.

Want to see how this can be accomplished in a single step? Look at the following code sample, which redefines the “Div” class that you saw before:

class Div extends HtmlElement implements Parser


public function parseContent()


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


$this->content = preg_replace(array('/(<html>|<body>)/', '/(</html>|</body>)/'), array('<div>', '</div>'), $this->content);


return $this;


// render div element

public function render()


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




By examining the definition of the improved “Div” class, you can see how easy it is to build polymorph objects that inherit structure and functionality from an interface and an abstract class. Please, notice how this concrete class implements the inherited “parseContent()” method to replace some invalid tags with divs, thus basically sanitizing its contents.

Now that you've grasped the driving logic of the previous class, it’s time to see how it can be used for displaying a single div on the browser. The following script shows how to do that:


// create new instance of Div class

$div = new Div();

// assign attributes and content for div element and display it on the browser

echo $div->setId('divid')


->setContent('<html>This is the new content for the div.</html>')



Here you have it. With this basic example I showed that constructing polymorph classes that inherit methods from an abstract class and one or more interfaces is indeed a straightforward process.

As usual, feel free to tweak all of the code samples included in this tutorial, so you can sharpen your Polymorphism skills in PHP 5.

Final thoughts

This is it for the moment. Over the course of this fifth chapter of the series, you learned how to build a polymorph object in PHP 5 by combining an abstract class and a simple interface, which was useful for constructing basic HTML divs.

In the next tutorial I’m going to show you how to construct a polymorph class using the previous approach, but this time the class will be responsible for rendering HTML paragraphs.

Don’t miss the upcoming article!

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