Jump to content

member variables or keepem in the body?


karpatzio

Recommended Posts

I wrote a simple template class for educational purposes (im a total newbee to php and programming in general) thats based on an article i read, anyway unlike the article i added a member variable $template to store the parsed data instead of storing it in the body of the application. i doubt either way would have areal impact in a simple app using this class but still i was wondering why did the author chose to handle the data in the body for the sake of simplicity in a code meant to be delivered to the unexperienced such as me or if it is by principle more efficient/correct?

 

the code:

class mTemplate
{
private var $args;
private var $fileRoot;
private var $template; // Holds the template to be written

/*******************************************************************
Constructor                                                        
*******************************************************************/

function __construct($templateDir = NULL)
{
	$this->fileRoot = $templateDir;
	$this->args = array();
	$this->template = '';
}

/*******************************************************************
Public Methods
*******************************************************************/

public function setPath($fileRoot)
{
	$this->fileRoot = $fileRoot;
}

public function setVars($var, $arg = NULL)
{
	if(is_array($var))
	{	
		if($arg)
		{
			$this->args = $var;
		} else {
			$this->args = array_merge($this->args, $var);
		}
	} else {
		$this->arg['$var'] = $arg;
	}

}

public function returnParsed($file, $args = NULL)
{
	if(is_array($args))
	{
		extract($args);
	} else {
		extract($this->args);
	}

	ob_start();
	include($this->fileRoot . $file);
	$contents = ob_get_contents();
	ob_end_clean(); // Close buffer, dont send to client
	return $contents;
}

public function addParsed($file, $args = NULL)
{
	if(is_array($args)
	{
		extract($args);
	} else {
		extract($this->args);
	}

	ob_start();
	include($this->fileRoot . $file);
	$contents = ob_get_contents();
	ob_end_clean(); // Close buffer, dont send to client
	$this->template .= $contents;
}

public function addUnparsed($bit)
{
	if(file_exists($this->fileRoot . $bit))
	{
		$this->template .= file_get_contents($this->fileRoot . $bit);
	} else {
		$this->template .= $bit;
	}

}

public function write()
{
	echo $this->template;
}

public function get()
{
	return $this->template;
}

public function delete()
{
	$this->template = '';
}

}

Link to comment
https://forums.phpfreaks.com/topic/106270-member-variables-or-keepem-in-the-body/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.