Home arrow PHP arrow Page 2 - Rendering HTML Paragraphs with Polymorphs in PHP 5

Review: constructing polymorph objects via interfaces - PHP

In this second article in a seven-part series on building polymorphs in PHP 5, I provide you with another example of how to do this by way of a simple interface. For this tutorial, I decided to work with objects that are responsible for rendering different HTML elements, such as div and paragraphs, but naturally itís possible to construct these types of object for different purposes as well.

  1. Rendering HTML Paragraphs with Polymorphs in PHP 5
  2. Review: constructing polymorph objects via interfaces
  3. Building a polymorph class that constructs HTML paragraphs
  4. A polymorph class in action
By: Alejandro Gervasio
Rating: starstarstarstarstar / 1
January 11, 2010

print this article



Before I begin explaining how to render HTML paragraphs by using polymorph objects that implement a determined interface, it'd be helpful to recall the example developed in the previous installment of the series. That example demonstrated how to work with this kind of object for building HTML divs.

Having said that, here's the definition of the interface that outlines the structure of generic HTML elements. Take a look at it:


interface HtmlElement


// assign id attribute to HTML element

public function setId($id = '');


// assign class attribute to HTML element

public function setClass($class = '');


// set content for HTML element

public function setContent($content = '');


// render HTML element

public function render();


As shown above, the structure of the "HtmlElement" interface is extremely simple to follow. It only defines four generic methods for assigning the "id" and "class" attributes to a given HTML element, as well as for setting its contents and rendering it to the browser.

As you may know, due to the abstract nature of interfaces in PHP 5, there's not much more that can be said about this one in particular, so it's time to show the definition of the class that's actually an implementer of the earlier "HtmlElement."

Here's the complete source code of this polymorph class, which is charged with building HTML divs:


class Div implements HtmlElement


private $id = 'divid';

private $class = 'divclass';

private $content = 'Default content for the div';


// assign id attribute to div element

public function setId($id = '')


if ($id !== '')


$this->id = $id;


return $this;



// assign class attribute to div element

public function setClass($class = '')


if ($class !== '')


$this->class = $class;


return $this;



// set content for div element

public function setContent($content = '')


if ($content !== '')


$this->content = $content;


return $this;



// render div element

public function render()


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




Well, you'll have to agree with me that the driving logic of the above "Div" class is very easy to grasp. All it does is implement the four methods declared by the "HtmlElement" interface to construct div elements.

Despite the rather primitive definition of this sample class, it demonstrates how interfaces can be used in PHP 5 for building polymorph objects in a truly painless way.

And now that you hopefully understand how the "Div" class does its thing, here's a code fragment that shows how to use it for displaying a simple div on screen:


// 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('myid')


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


There you have it. Thanks to the chainable API exposed by the "Div" class, rendering an HTML div on screen is as simple as writing a couple of lines of code, aside from the corresponding comments.

Well, at this point you may have realized how useful interfaces can be for  achieving Polymorphism in PHP 5. So, next I will develop another example similar to the previous one, but this time the class that will implement the previous "HtmlElement" interface will be responsible for displaying some HTML paragraphs.

Want to learn how this will be accomplished? Then simply click on the link below and read the following section.

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