HomePHP Page 2 - PHP: View Object Collections and the Composite View Design
Review: building an abstract composite view class - PHP
In this second part of a series, I demonstrate how to create a concrete composite view class, capable of rendering single and multiple view templates via the same “render()” method. The definition of this brand new class not only shows the logic that drives the Composite View pattern, but reveals how the pattern takes advantage of the benefits offered by Composition.
As usual, before I start showing how to create a concrete composite view class that can manipulate single and multiple view objects via the same interface, I’d like to spend a few moments reintroducing the definition of the abstract parent created in the first part of this series.
Here’s how this abstract class was originally conceived:
// constructor public function __construct($template = '', array $data = array()) { if ($template !== '') { $this->setTemplate($template); } if (!empty($data)) { foreach ($data as $name => $value) { $this->$name = $value; } } }
// set a new view template public function setTemplate($template) { $template = $template . '.php'; if (!file_exists($template)) { throw new ViewException('The specified view template does not exist.'); } $this->_template = $template; }
// get the view template public function getTemplate() { return $this->_template; }
// set a new property for the view public function __set($name, $value) { $this->_properties[$name] = $value; }
// get the specified property from the view public function __get($name) { if (!isset($this->_properties[$name])) { throw new ViewException('The requested property is not valid for this view.'); } return $this->_properties[$name]; }
// remove the specified property from the view public function __unset($name) { if (isset($this->_properties[$name])) { unset($this->_properties[$name]); } }
// add a new view (implemented by view subclasses) abstract public function addView(AbstractView $view);
// remove a view (implemented by view subclasses) abstract public function removeView(AbstractView $view);
// render the view template public function render() { if ($this->_template !== '') { extract($this->_properties); ob_start(); include($this->_template); return ob_get_clean(); } } }// End AbstractView class
<?php
class ViewException extends Exception{}
If you are familiar with the “__set()” and “__get()” magic PHP 5 methods, the logic driving the previous “AbstractView” class should be pretty easy for you to grasp, as it uses them to set and retrieve undeclared properties from a view object. Also, the class implements an additional method called “render()” to populate the template associated with each view with these properties. That was fairly simple to follow, right?
Even though all of the concrete methods just explained do play a relevant role, the most interesting ones are “addView()” and “removeView().” They define the interface required for dealing transparently with single and multiple view objects. Since the earlier abstract parent defined the methods in question as abstract, they must be concretely implemented by a subclass (this is Inheritance 101).
That’s precisely what I’m going to do in the following segment. To learn more about this process, click on the link below and keep reading.