Hi guys,
I am a bit worried about my simple template script. I know that using extract can be dangerous.. but since I am not receiving any users input
I decided to go along with using it in this case. What I want to know is, am I using get/set () correctly here? I mean is it worth using it this way?
I really don't want to release scripts if there is bad coding practice all over. The reason why I am doing it this way is because I like the idea of just having to set something and just calling its $name on a script.
if (!class_exists ("template"))
{
class template
{
protected $templateDir;
protected $templateFile;
protected $templateVars = array ();
public function __construct ($dir)
{
if (!is_dir ($dir))
{
throw new Exception ("$dir no such file or directory", 1);
}
$this->templateDir = $dir;
}
public function stgRenderTemplate ($file)
{
if (!is_readable ($this->templateDir.$file))
{
throw new Exception ("$this->templateDir.$file File is not readable", 1);
}
$this->templateFile = $file;
}
public function stgOutputTplView ()
{
$bufferData = "";
ob_start ();
if (!empty ($this->templateVars))
{
extract ($this->templateVars);
}
include $this->templateDir.$this->templateFile;
$bufferData = ob_get_clean ();
return $bufferData;
}
public function __get ($p)
{
if (array_key_exists ($p, $this->templateVars))
{
return $this->templateVars [$p];
}
}
public function __set ($p, $v)
{
$this->templateVars [$p] = $v;
}
}
}
I just really want to know if this is good and that maybe I should stop worrying. This is being coded for a wp plugin.