Now that I've defined a basic interface with the bare bones structure of a generic HTML element, it's time create a class that implements the set of methods declared by this interface. As you'll see in a moment, these methods will allow it to build polymorph objects very easily. That being explained, here's the implementer class of the previous "HtmlElement" interface. Its function is to construct basic divs. Take a look at the partial source code of this class, please: 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; } } As depicted above, the new "Div" class gives a concrete implementation to the "setId()" and "setClass()" methods defined at the top of the hierarchy, a process that demonstrates in a nutshell how to take advantage of Polymorphism in PHP 5 by using a simple interface. However, as you may know, when a class implements an interface, it must do this with all of the interface's methods. That isn't the case with the above "Div" class, since no logic has been added yet to the "setContent()" and render()" methods. Obviously, it's necessary to address this issue as soon as possible. Therefore, below I've included the complete definition of the "Div" class, this time implementing the aforementioned methods. Here it is: 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>';
} } Certainly, you'll have to agree with me that the definition of the "Div" class now looks much clearer. The corresponding "setContent()" and "render()" methods have been specifically implemented to construct an HTML div element. Despite the simplicity of this sample class, it's useful for showing how easy it is to build polymorph classes in PHP 5 by inheriting the set of methods declared by a certain interface. So far, so good. Now that you've hopefully grasped the underlying logic of the earlier "Div" class, I'm going to set up an example that demonstrates the actual functionality of the class. Bearing that idea in mind, in the following section I'm going to develop that example for you, which will be an adequate conclusion for this first part of the series. Now read the next segment. I'll be there, waiting for you.
blog comments powered by Disqus |
|
|
|
|
|
|
|