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.
blog comments powered by Disqus |
|
|
|
|
|
|
|