I want to run a template engine on the same file as the file I need it to run on, so I have the template engine that makes things such as {username} turn into a PHP variable in the HTML, be on the same page as everything that handles that. But if I try to do that, I get a class has already been called error, I have tried fixing this error, but this is the closest I have gotten, is there anyway to fix this error? Here is my code:
<?php //error_reporting(0); if(in_array('NewTemplateHandler', get_declared_classes())) { $template = new NewTemplateHandler("test4.php"); $template->output(); //echo "Template has not been called"; }else { echo "Else called"; } class NewTemplateHandler { protected $file; protected $values = array(); public function __construct($file) { //$_GLOBALS["ran"] = true; $this->file = $file; $this->__set("test", "Template Working"); echo "Template Class Called."; } public function __set($key, $value) { $this->values[$key] = $value; echo "$key set as $value<br />"; } public function output() { if(!file_exists($this->file)) { echo "ERROR: Template file not found."; } $output = file_get_contents($this->file); foreach($this->values as $key => $value) { $find = "{".$key."}"; $output = str_replace($find, $value, $output); } //$_GLOBALS["ran"] = true; return eval("?>".$output); } } ?> <br /> Thing-> {test}
if(in_array('CLASS NAME', get_declared_classes())) {.....is what you want.include_oncewhen including class files, it will take care rest of the things itself.