Home arrow PHP arrow Page 3 - Using Abstract Classes to Build Polymorphs in PHP 5

Implementing Polymorphism via abstract classes - PHP

In this third part of a seven-part series, I demonstrate how to build polymorph objects using abstract classes in PHP 5. As you'll see, it's a pretty straightforward process that can be tackled with relative ease.

TABLE OF CONTENTS:
  1. Using Abstract Classes to Build Polymorphs in PHP 5
  2. Review: building polymorph objects using interfaces
  3. Implementing Polymorphism via abstract classes
  4. Using the Div subclass
By: Alejandro Gervasio
Rating: starstarstarstarstar / 2
January 12, 2010

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

Without a doubt, one of the simplest approaches that can be utilized for building polymorph objects in PHP relies on using parent classes. Basically, this approach requires building a base class on top of the hierarchy, and then deriving from this parent as many subclasses as needed, which permits you to implement in a different manner a method that is common to all of the children.

To grasp this approach more clearly, the first thing that I’m going to do will be defining an abstract class that will encapsulate most of the functionality required for building basic HTML elements.

Once this parent has been properly built, a child class will be spawned from it, which will be specially refined for rendering HTML divs. Not too difficult to grasp, right?

Having outlined my plan, here’s the definition of the aforementioned abstract class. Take a look at it:

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

}

Certainly, understanding how this abstract class does its business isn’t rocket science. As you can see, it looks very similar to the sample classes shown in the previous segment, with the exception of some subtle differences worth mentioning here. First, this is an abstract class, so it can’t be instantiated. And second, it does implement its two setters, but no logic has been added to its “render()” method.

This makes it really easy to derive a subclass from “HtmlElement” that gives a concrete implementation to this particular method. That’s exactly what the following “Div” class does. Look at it:

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

 

}

}

Since the abstract “HtmlElement” class encapsulates almost all the functionality required for constructing simple HTML element objects, the definition of its subclass “Div” is actually very short. In this case, the “render()” method is implemented only with the purpose of returning to client code the HTML necessary to display a div on screen.

At this stage everything looks pretty good. I recreated a typical scenario where a subclass is responsible for adding logic to a method defined by its corresponding parent, which happens to be an abstract class. Nonetheless, at this point it’d be premature to say that this subclass is truly a polymorph, since it’s necessary to derive another child class that implements the “render()” method in a different way.

But actually I’m getting ahead of myself, since this process will be tackled in the upcoming part of the series. Meanwhile, it’d be desirable to build a script that shows how to use the previous “Div” class.

Since that script will be coded in the final section, I suggest you read the following segment.



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