PHP Page 2 - Polymorphs in PHP: Using Interfaces and Abstract Classes to Construct HTML Paragraphs |
In the preceding part of this series I went through the development of a polymorph class that inherited its methods from an abstract class and from a single interface. As a review, below I included the complete source code corresponding to this example. Study the following code sample, please: // define interface Parser interface Parser { public function parseContent(); }
// 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(); } You shouldn't have much trouble understanding the purpose of defining the previous “Parser()” interface and the complementary “HtmlElement” abstract class. As shown above, when properly combined, these entities provide both functionality and structure for building simple HTML elements that can be easily rendered on the browser. However, to understand more clearly how these sample entities can be used for achieving Polymorphism in a simple manner, it’s necessary to define a class that implements the inherited “parseContent()” method. The following “Div” class performs this task in only one go: 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>';
} } There you have it. In this case, the “Div” class adds concrete logic to the “parseContent()” method, so it can be used for stripping unwanted tags from the contents assigned to div elements, such as <html> and <body>. Of course, it’s possible to make this method more complex, but for the moment I’ll keep it simple. Now that you've learned (or recalled) how the above “Div” class does its thing, here’s a short script that shows how to use it in a concrete case: // create new instance of Div class $div = new Div(); // assign attributes and content to div element and display it on the browser echo $div->setId('divid') ->setClass('divclass') ->setContent('<html>This is the new content for the div.</html>') ->parseContent() ->render(); While this code sample does demonstrate how to use the “Div” class for constructing a simple HTML div, it admittedly comes up short in showing the class’s polymorph nature. What’s wrong with this example? Well, nothing really, but to prove that this class is a polymorph structure, it’s necessary to build another one that displays a different behavior for the same “parseContent()” method. However, building that new class will be covered in the next section. So click on the link below and read the next few lines.
blog comments powered by Disqus |
|
|
|
|
|
|
|