Considering that the main task of the concrete factory that you just saw in the previous section is to create a couple of block-level (X)HTML objects, it’s necessary to define the classes that will originate the objects. With that in mind, below you'll see the definition of a whole new abstract class that models the structure and partial functionality of generic HTML objects. Here it is: (HtmlElement.php) <?php abstract class HtmlElement { protected $_class = ''; protected $_id = ''; protected $_content = '';
// constructor public function __construct(array $options = array()) { if (isset($options['class'])) { $this->setClass($options['class']); } if (isset($options['id'])) { $this->setId($options['id']); } if (isset($options['content'])) { $this->setContent($options['content']); } }
// set the 'class' attribute for the HTML element public function setClass($class) { if (is_string($class)) { $this->_class = $class; } return $this; }
// get the assigned 'class' attribute public function getClass() { return $this->_class; }
// set the 'id' attribute for the HTML element public function setId($id) { if (is_string($id)) { $this->_id = $id; } return $this; }
// get the assigned 'id' attribute public function getId() { return $this->_id; }
// set the content for the HTML element public function setContent($content) { if (is_string($content)) { $this->_content = $content; } return $this; }
// get the content of the HTML element public function getContent() { return $this->_content; } // implemented by concrete subclasses abstract public function render(); } Put in a simple way, the above “HtmlElement” class implements a few getters and mutators (aside from its constructor, of course) which retrieve and assign values to the “class” and “id” attributes of a skeletal HTML element. Besides, the element’s contents can be easily handled in a similar fashion via the “setContent()” and “getContent()” methods respectively. For obvious reasons, the “render()” method has been declared abstract, since it should be implemented by concrete subclasses that render specific HTML elements. Therefore, now that you know how this abstract class does its thing, it’s time to start spawning a couple of child classes, which will be tasked with creating some trivial divs and paragraphs. This process will be discussed in depth in the following section, so jump ahead and read the lines to come, please.
blog comments powered by Disqus |
|
|
|
|
|
|
|