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

Review: building polymorph objects by using abstract classes - 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.

TABLE OF CONTENTS:
  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
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

As I stated in the introduction, my plan for this tutorial consists of deriving a whole new child class from the parent that builds generic HTML elements, and making it capable of displaying paragraphs. So, it’d be useful to recall how the parent class was originally defined, along with the subclass that constructs divs.

Please take a look at the signature of the abstract “HtmlElement” parent class, which was developed in the following way:

abstract class HtmlElement

{

protected $id = 'myid';

protected $class = 'myclass';

protected $content = 'Default content for the HTML element';

 

// assign id attribute to HTML element

public function setId($id = '')

{

if ($id !== '')

{

$this->id = $id;

}

return $this;

}

 

// assign class attribute to HTML element

public function setClass($class = '')

{

if ($class !== '')

{

$this->class = $class;

}

return $this;

}

 

// set content for HTML element

public function setContent($content = '')

{

if ($content !== '')

{

$this->content = $content;

}

return $this;

}

 

// render HTML element (not implemented)

abstract function render();

}

As you can see, the definition of the above “HtmlElement” abstract class speaks for itself. It declares and implements some simple methods for constructing generic HTML elements. Despite the trivial source code of this particular class, it must be noted that its “render()” method has no concrete implementation.

You may be wondering what’s the point is in doing this. Well, the answer is really simple: if several subclasses provide a refined implementation for the method, then it’s possible to create a set of children that build all sorts of HTML elements. It's simple and efficient.

The following “Div” class adds to the “render()” method the logic required for creating divs. Here it is:

// define Div class (subclass of HtmlElement class)

class Div extends HtmlElement

{

// render div element

public function render()

{

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

 

}

}

Done. Now, there’s a subclass that’s capable of rendering divs on the browser. Don’t believe me? Then give the script below a try and see the output that it produces:

// 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')->setClass('divclass')->setContent('This is the new content for the div.')->render();

There you have it. As you can see, once an instance of the “Div” class has been created, it’s extremely easy to display a div on the browser, thanks to its chainable API.

The most important detail here, though, is that this class displays a determined behavior when its “render()” method is invoked. But what happens if I derive another subclass from the parent “HtmlElement” and make the same method capable of rendering paragraphs?

That would be pretty interesting, since there would be two classes that belong to the same family but behave differently. Hey, that’s exactly the characteristic that distinguishes polymorphs from regular objects!

Naturally, to demonstrate this concept it’s necessary to create the subclass that will build HTML paragraphs. This process will be covered in depth in the following section, so click on the link below and keep reading.



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

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PHP ARTICLES

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