limit scope

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

Post Reply
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

limit scope

Post by Luke »

I don't know if I'm having a brain fart, or if I just don't know how to accomplish this... I would like to restrict a method's access to member variables and other methods. For example... I have a render() method in a View class (yes, this is for my Calendar if you're wondering) and I would like to make sure it doesn't have access to variables it doesn't need look at my comment inside of the render() class...

Code: Select all

<?php
class MC2_Calendar_View_Template extends MC2_Calendar_View{

	protected $template;
	protected $data = array();
	
	public function __construct($template=null){
	
		if(!is_null($template)) $this->template = $template;
		
	}
	
	public function setTemplate($template){
	
		$this->template = $template;
		
	}
	
	public function assign($key, $value){
	
		$this->data[$key] = $value;
		
	}
	
	public function render(){
	
        /* This is the area I am talking about. Do you see how I only am giving the template
           access to specific variables? How do I restrict this access so that you can't just grab
           variables by $this->calendar->month->fullName() from within the template? */
            
		// Assign allowed variables from month and calendar
		$grid = $this->calendar->getGrid();
		$month = $this->calendar->month->fullName();
		$week = $this->calendar->week;
		$totalDays = $this->calendar->month->totalDays();
		
		// Extract the user-supplied data
		extract($this->data);
		
		// Render the template
		ob_start();
		include $this->template;
		return ob_get_clean();
		
	}
	
}
?>
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post by Christopher »

If you do not want the view properties and methods available you should probably make you template a separate class rather than extending the View. I usually composite the Template within the View for this reason.
(#10850)
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Post by Ollie Saunders »

Code: Select all

public function render(){
    $template = new $this->template;
    $template->addParams(array('grid'      => $this->calendar->getGrid(),
                               'month'     => $this->calendar->month->fullName(),
                               'week'      => $this->calendar->week,
                               'totalDays' => $this->calendar->month->totalDays());
    return $template->render();
} 

class Template
{
    private $_params;
    private $_fileName;
    public function __construct($fileName)
    {
        $this->_fileName = fileName;
    }
    public function addParams(array $params)
    {
        // merge makes multiple calls to addParams possible
        $this->_params = array_merge($params, $this->_params);
    }
    public function render()
    {
        extract($this->_params);
        ob_start();
        include $this->_filename;
        $out = ob_get_contents();
        ob_end_clean();
        return $out;
    }
}
Post Reply