In the previous article I discussed how to build a couple of polymorph objects by using a simple interface; below, I listed the source code of that interface, along with the pair of sample classes that implement its generic methods. First, take a look at the definition of the interface, which comprises the bare bones structure of an HTML element. Here it is: 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 you can see, there’s nothing special or spectacular about the way that the “HtmlElement” interface has been defined, because all that it does is declare four methods, which of course must be implemented by one or more classes to render specific HTML elements. Now that you've learned how this interface was built initially, it’s time to show the definitions of the classes that are implementers of it. The first one is responsible for displaying divs, while the second one is charged with building basic paragraphs. Here’s the signature of the first of these classes, not surprisingly called “Div:” 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>';
} } If you take a closer look at the above class, then you’ll realize how it works. It implements the four generic methods defined by the “HtmlElement” interface for constructing simple divs. Undoubtedly, analyzing the “Div” class on its own is a pretty boring process that doesn’t deserve any special attention. However, things get much more interesting if you look at the definition of the following class, which is also an implementer of the “HtmlElement” interface and uses its methods for constructing some paragraphs. Here’s the referenced class: class Paragraph implements HtmlElement { private $id = 'parid'; private $class = 'parclass'; private $content = 'Default content for the paragraph';
// assign id attribute to paragraph element public function setId($id = '') { if ($id !== '') { $this->id = $id; } return $this; }
// assign class attribute to paragraph element public function setClass($class = '') { if ($class !== '') { $this->class = $class; } return $this; }
// set content for paragraph element public function setContent($content = '') { if ($content !== '') { $this->content = $content; } return $this; }
// render paragraph element public function render() { return '<p id="' . $this->id . '" class="' . $this->class . '">' . $this->content . '</p>';
} } In this example, the “Paragraph” class is responsible for displaying HTML paragraphs on screen and nothing else. Regardless of the rather simple logic that drives this particular class and its counterpart “Div,” though, the most important thing to stress here is that even when both are implementers of the “HtmlElement” interface, they behave differently when their “render()” method gets called. This shows how easy it is to achieve Polymorphism in PHP 5 using an interface. You'll understand the process better if you look at the following script, which renders a div and a paragraph on the browser by creating a couple of objects from the aforementioned classes: // 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();
// 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('myid') ->setClass('myclass') ->setContent('This is the new content for the paragraph.') ->render(); That was easy to code and read, wasn’t it? This code fragment shows clearly how two objects that implement the same “render()” method can behave radically different at runtime, which turns them into true polymorph objects. Nonetheless, as I mentioned in the introduction, interfaces aren’t the only approach that can be used for achieving Polymorphism in PHP 5. It's also possible to utilize parent classes to take advantage of the functionality of Inheritance. If you’re interested in learning how to build polymorph objects via parent classes, in the next section I’m going to show you how to create the same div elements that you saw before, this time by using a base abstract class. To learn more on this topic, click on the link that appears below and keep reading.
blog comments powered by Disqus |
|
|
|
|
|
|
|