Without a doubt, one of the simplest approaches that can be utilized for building polymorph objects in PHP relies on using parent classes. Basically, this approach requires building a base class on top of the hierarchy, and then deriving from this parent as many subclasses as needed, which permits you to implement in a different manner a method that is common to all of the children. To grasp this approach more clearly, the first thing that I’m going to do will be defining an abstract class that will encapsulate most of the functionality required for building basic HTML elements. Once this parent has been properly built, a child class will be spawned from it, which will be specially refined for rendering HTML divs. Not too difficult to grasp, right? Having outlined my plan, here’s the definition of the aforementioned abstract class. Take a look at it: 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(); } Certainly, understanding how this abstract class does its business isn’t rocket science. As you can see, it looks very similar to the sample classes shown in the previous segment, with the exception of some subtle differences worth mentioning here. First, this is an abstract class, so it can’t be instantiated. And second, it does implement its two setters, but no logic has been added to its “render()” method. This makes it really easy to derive a subclass from “HtmlElement” that gives a concrete implementation to this particular method. That’s exactly what the following “Div” class does. Look at it: // define Div class (subclass of HtmlElement class) class Div extends HtmlElement { // render div element public function render() { return '<div id="' . $this->id . '" class="' . $this->class . '">' . $this->content . '</div>';
} } Since the abstract “HtmlElement” class encapsulates almost all the functionality required for constructing simple HTML element objects, the definition of its subclass “Div” is actually very short. In this case, the “render()” method is implemented only with the purpose of returning to client code the HTML necessary to display a div on screen. At this stage everything looks pretty good. I recreated a typical scenario where a subclass is responsible for adding logic to a method defined by its corresponding parent, which happens to be an abstract class. Nonetheless, at this point it’d be premature to say that this subclass is truly a polymorph, since it’s necessary to derive another child class that implements the “render()” method in a different way. But actually I’m getting ahead of myself, since this process will be tackled in the upcoming part of the series. Meanwhile, it’d be desirable to build a script that shows how to use the previous “Div” class. Since that script will be coded in the final section, I suggest you read the following segment.
blog comments powered by Disqus |
|
|
|
|
|
|
|