Home arrow PHP arrow Page 3 - Creating a Subclass for Building Polymorphs in PHP

Building another polymorph subclass - PHP

Welcome to the fourth installment of an article series on building polymorphs in PHP. Made up of seven tutorials, this series attempts to teach you through numerous, comprehensive code samples how to create polymorph objects. You'll see how to do it using interfaces, then abstract classes, and finally a combination of both.

  1. Creating a Subclass for Building Polymorphs in PHP
  2. Review: building polymorph objects by using abstract classes
  3. Building another polymorph subclass
  4. The functionality of a polymorph object
By: Alejandro Gervasio
Rating: starstarstarstarstar / 2
January 13, 2010

print this article



As I explained in the section that you just read, to demonstrate that many of the subclasses derived from the parent “HtmlElement” can be considered true polymorph entities, it’s necessary to build a class that behaves radically different when its “render()” method gets called.

With that requirement in mind, below I included the definition of this brand new subclass. It's charged with building HTML paragraphs. Look at it, please:

// define Paragraph class (subclass of HtmlElement class)

class Paragraph extends HtmlElement


// render paragraph element

public function render()


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




Were you expecting to be confronted with a complex and lengthy class? Well, fortunately for you and me this isn’t the case here. The task that the “Paragraph” class performs is constructing HTML paragraphs via its inherited “render()” method.

Now that this new subclass has been completely defined, it should be clear for you to see its polymorph nature, particularly when compared with its sister “Div.” These two child classes are of the same “HtmlElement” type, but each of them acts radically different when rendering a specific HTML element.

At this point, you’ve hopefully grasped the logic that stands behind building polymorph objects using an abstract class in PHP 5. But, there’s still one thing that remains undone -- developing a script that shows how to use the previous “Paragraph” class.

That script will be built in the last part of this tutorial. Therefore, click on the link that appears below and read the next few lines.

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