Home arrow PHP arrow Page 4 - PHP Composite View Design Pattern: Introducing the Key Concepts

Finish sample class: add a method for rendering view templates - PHP

The Composite View design pattern offers users the versatility of handling single objects and collections through the same interface. But some programmers hesitate to use it due to its tricky implementation. Hesitate no longer; this multi-part series will take you step by step through some of the best ways to use the Composite View design pattern, complete with code samples to show you the way.

  1. PHP Composite View Design Pattern: Introducing the Key Concepts
  2. Defining an abstract composite view class
  3. Defining methods for adding and removing views
  4. Finish sample class: add a method for rendering view templates
By: Alejandro Gervasio
Rating: starstarstarstarstar / 3
August 17, 2010

print this article



To complete the earlier abstract composite view class, we must add to it a method that allows it to render its associated template on screen. This simple method will be called, not surprisingly, “render().” Its implementation has been included below in the class’s source code. Check it out:    



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

    // constructor
    public function __construct($template = '', array $data = array())
        if ($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])) {
    // 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 !== '') {
           return ob_get_clean();
}// End AbstractView class

As you can see above, the “render()” method is responsible for performing two well-differentiated tasks. First, it populates the view template with the values assigned to the class properties created “magically,” and second, it returns the parsed template to client code for further processing. Of course, it’s clear to see that the logic of this method can be improved in many clever ways, but for the sake of clarity I’m going to keep it this simple.

Now that the development of the “AbstractView” class has been completed, it should be a bit easier for you to understand how concrete implementations of this class will be capable of rendering one and composite views through the same “render()” method. However, if this process still remains confusing to you, don’t worry; it’ll be covered in detail in the next tutorial. 

Final thoughts

In this introductory part of the series, I went through the definition of an abstract composite view class. This class not only encapsulates the functionality of generic view objects, but defines the interface required for adding and removing the objects in question. Naturally, this capability must be implemented in some way by a concrete subclass, right?

To fit this requirement, in the upcoming tutorial I’m going to create the aforementioned subclass. This should help you understand more clearly how to render single and multiple views via the “render()” method implemented by the abstract parent created before.

Don’t miss the next part!

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

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


- 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: