variable scope for view

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

variable scope for view

Post by Luke »

I am building an application right now and I am throwing up a quick-and-dirty framework for it because I just don't have the time to do much more. I have a simple function for capturing a file's html into a variable to use for my view:

Code: Select all

function capture_file($file){
	ob_start();
	include $file;
	return ob_get_clean();
}
The problem is that variables declared in the $file are inside of this function and therefor have its scope. What can I do to acheive this same functionality, but not have to send capture_file all of the variable that $file needs... ?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Are the variables it needs globals? If so, your own version of extract() could probably be useful.

Otherwise, it'll have to be some variant on that theme or you'll need to specify more specific context.
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post by Luke »

no... the variables it needs are any dynamic content within that view... such as: "hello, $username"
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: variable scope for view

Post by Christopher »

I tend to do it like this to keep vars out of the local namespace:

Code: Select all

class Template_Php {
var $file = '';
var $tags = array();

function set($tag, $value) {
	$this->tags[$tag] = $value;
}

function render(){
	ob_start();
	extract($this->tags);
	include $this->file;
	return ob_get_clean();
}

}
(#10850)
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post by Luke »

good call... this is the only part of the framework where I wasn't using OOP... I will just convert what I've already done to use a view object. Thanks buddy.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

Code: Select all

class Template
{
   public function __construct() { }
 
   public function __set($name, $value)
   { 
      if (isset($this->$name)) {
         //spit error about variable existing already
      }
  
      $this->$name = $val;
   }   

   public function capture_file($file)
  {
        ob_start();
        include $file;
        return ob_get_clean();
   }
}

Code: Select all

<html>
   <body>
      <?php echo $this->somevar; ?>
   </body>
</html>

Code: Select all

$template = new Template();
$template->title = 'Home';
You can take advantage of the __set method in this case, or just use another method to register variables to the class to give your template file the variable scope. There are so many different ways, just play around for a bit.

Edit | Damn I was late :oops:
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post by Luke »

can't use __set cuz it's php4. (which is really why I am just kind of throwing it together... I have a sneaking suspicion I will have to rebuild it anyway so I can merge it with a php5 project... man I love this stuff!! Image
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

create a setter method then

Code: Select all

public function setvar($name, $value)
   {
      if (isset($this->$name)) {
         //spit error about variable existing already
      }
 
      $this->$name = $value;
   }

Code: Select all

$template = new Template();
$template->setvar('title', 'Home');
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post by Luke »

This is what I am using for now... I will spruce it up when I can get around to it.

Code: Select all

class View{
	var $_vars = array();
	function set($name, $value){
		$this->_vars[$name] = $value;
	}
	function render($file){
	//TODO: check that $file is really a file
		ob_start();
		extract($this->_vars);
		include $file;
		return ob_get_clean();
	}
}
Post Reply