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