Before I begin explaining how to render HTML paragraphs by using polymorph objects that implement a determined interface, it'd be helpful to recall the example developed in the previous installment of the series. That example demonstrated how to work with this kind of object for building HTML divs. Having said that, here's the definition of the interface that outlines the structure of generic HTML elements. Take a look at it:
interface HtmlElement { // assign id attribute to HTML element public function setId($id = '');
// assign class attribute to HTML element public function setClass($class = '');
// set content for HTML element public function setContent($content = '');
// render HTML element public function render(); } As shown above, the structure of the "HtmlElement" interface is extremely simple to follow. It only defines four generic methods for assigning the "id" and "class" attributes to a given HTML element, as well as for setting its contents and rendering it to the browser. As you may know, due to the abstract nature of interfaces in PHP 5, there's not much more that can be said about this one in particular, so it's time to show the definition of the class that's actually an implementer of the earlier "HtmlElement." Here's the complete source code of this polymorph class, which is charged with building HTML divs:
class Div implements HtmlElement { private $id = 'divid'; private $class = 'divclass'; private $content = 'Default content for the div';
// assign id attribute to div element public function setId($id = '') { if ($id !== '') { $this->id = $id; } return $this; }
// assign class attribute to div element public function setClass($class = '') { if ($class !== '') { $this->class = $class; } return $this; }
// set content for div element public function setContent($content = '') { if ($content !== '') { $this->content = $content; } return $this; }
// render div element public function render() { return '<div id="' . $this->id . '" class="' . $this->class . '">' . $this->content . '</div>';
} } Well, you'll have to agree with me that the driving logic of the above "Div" class is very easy to grasp. All it does is implement the four methods declared by the "HtmlElement" interface to construct div elements. Despite the rather primitive definition of this sample class, it demonstrates how interfaces can be used in PHP 5 for building polymorph objects in a truly painless way. And now that you hopefully understand how the "Div" class does its thing, here's a code fragment that shows how to use it for displaying a simple div on screen:
// 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('myid') ->setClass('myclass') ->setContent('This is the new content for the div.') ->render(); There you have it. Thanks to the chainable API exposed by the "Div" class, rendering an HTML div on screen is as simple as writing a couple of lines of code, aside from the corresponding comments. Well, at this point you may have realized how useful interfaces can be for achieving Polymorphism in PHP 5. So, next I will develop another example similar to the previous one, but this time the class that will implement the previous "HtmlElement" interface will be responsible for displaying some HTML paragraphs. Want to learn how this will be accomplished? Then simply click on the link below and read the following section.
blog comments powered by Disqus |
|
|
|
|
|
|
|