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

Achieving Polymorphism via abstract classes - 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.

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

TOOLS YOU CAN USE

advertisement

As I explained in the introduction, PHP 5 makes it really easy to implement Polymorphism by combining the functionality of abstract classes and interfaces. But before I dive more deeply into the details of how to accomplish this, I’d like to spend a few moments reintroducing the example application developed in the previous article of the series. It showed how to build a couple of polymorph objects by way of a simple abstract class.

That being clarified, here’s the definition of that abstract class, which encapsulates most of the functionality required for creating HTML elements. Look at it, please:

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 shown before, the signature of the “HtmlElement” abstract class includes nothing special. It simply defines some basic methods for assigning “id” and “class” attributes to the HTML element being created, as well as for setting its content.

Of course, the class’s “render()” method has no concrete implementation, since this process should be delegated entirely to the eventual subclasses. Below you'll find the signatures of two child classes that use the method for displaying paragraphs and a div, respectively, on screen. Here they are:

// 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>';

 

}

}

 

// 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>';

 

}

}

As I stated previously, the pair of subclasses shown above add concrete logic to the inherited “render()” method for constructing HTML divs and paragraphs respectively. This demonstrates that two classes that are of the same type can behave differently when the same method is called.

If you have any doubts about the polymorph nature of these classes, then take a look at the following script, which should help to dissipate them quickly:

// 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();

 

// create new instance of Paragraph class

$par = new Paragraph();

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

echo $par->setId('parid')->setClass('parclass')->setContent('This is the new content for the paragraph.')->render();

Simple and illustrative. From the previous code fragment it’s clear to see how easy it is to build polymorph objects by using an abstract class. This is by far the most common approach that can be used for implementing Polymorphism in PHP 5. As I discussed in a previous part of this series, it’s also possible to use interfaces, even though this approach doesn’t fully exploit the benefits of Inheritance, for obvious reasons.

Nonetheless, as this article’s title suggests, it’s feasible to construct polymorph objects by combining abstract classes and interfaces with relative ease. So, in the next section I’m going to demonstrate how to do this with some of the sample classes shown previously.

To learn more on this topic, 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: