Naturally, before I demonstrate how the pair of polymorph classes built in the previous tutorial can be used within the same script, it’d be helpful to show their respective definitions and reintroduce the signature of the interface and the abstract parent class from which they inherit both structure and functionality. Here’s source code of the interface that declares a single method called “parseContent().” Take a look at it: // define interface Parser interface Parser { public function parseContent(); }
As I said before, the “Parser()” interface declares the signature of only one method, so there’s not much to be said about it for the moment. Let's look at the definition of the following abstract parent class, which encapsulates enough functionality to build generic HTML elements: // define abstract class HtmlElement 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 above “HtmlElement” abstract class simply implements a few straightforward methods for outlining the behavior of generic HTML elements, so I won’t spend additional time explaining the details of its driving logic. Instead, now that you hopefully recalled how this class functions, let me show you the definitions of the polymorph classes that implement the earlier “Parser” interface and are also derived from the parent class that you just saw. Here they are: class Div extends HtmlElement implements Parser { public function parseContent() { if ($this->content !== '') { $this->content = preg_replace(array('/(<html>|<body>)/', '/(<\/html>|<\/body>)/'), array('<div>', '</div>'), $this->content); } return $this; } // 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 { public function parseContent() { if ($this->content !== '') { $this->content = str_replace(array('</p>','</p>)'), "", $this->content);
} return $this; } // render paragraph element public function render() { return '<p id="' . $this->id . '" class="' . $this->class . '">' . $this->content . '</p>';
} } Definitely, grasping the driving logic of the two polymorph classes isn’t rocket science, right? As you can see, they’re simple implementers of the pertinent “Parser” interface, in addition to being subclasses of the parent “HtmlElement.” This condition lets them take advantage of these two structures simultaneously. Not bad, eh? So far, so good. At this stage, you're hopefully familiar with the way that these two polymorph classes do their business. So it’s time to develop some sample scripts that show how to use them in tandem for rendering HTML divs and paragraphs. In the section to come, I’m going to create the first of those scripts, so read the following segment. I’ll be there, waiting for you.
blog comments powered by Disqus |
|
|
|
|
|
|
|