Home arrow PHP arrow 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.

TABLE OF CONTENTS:
  1. PHP: View Object Collections and the Composite View Design
  2. Review: building an abstract composite view class
  3. Adding and removing view objects
  4. Adding an enhanced render() method
By: Alejandro Gervasio
Rating: starstarstarstarstar / 1
August 23, 2010

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

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: 

(AbstractView.php)


<?php

abstract class AbstractView
{
    protected $_template = 'default.php';
    protected $_properties = array();

    // 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.



 
 
>>> More PHP Articles          >>> More By Alejandro Gervasio
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PHP ARTICLES

- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: