0

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} 
7
  • 1
    if(in_array('CLASS NAME', get_declared_classes())) {..... is what you want. Commented Oct 8, 2014 at 4:10
  • @Darren Thank you so much, it worked perfectly, I did have to add an error_reporting(0); to get rid of another class has been called error, but it does what I need it to do. Please write that as an answer so I can accept it. Commented Oct 8, 2014 at 4:17
  • @Darren the only problem is, I had to put it at the top of the page, and it is repeating itself infinitely. Commented Oct 8, 2014 at 4:38
  • @M4TRIX, you should use include_once when including class files, it will take care rest of the things itself. Commented Oct 8, 2014 at 4:43
  • @ApulGupta I am trying to avoid those, I have put the class file inside the file I am trying to change. I have now changed my above code to match what I am currently having issues with. Commented Oct 8, 2014 at 4:44

1 Answer 1

2

This is what you need.

if(!in_array('NewTemplateHandler', get_declared_classes())) { $template = new NewTemplateHandler("test4.php"); $template->output(); } 

What you were doing before was this:

IF (TRUE) THEN START NEW TEMPLATE ELSE ECHO "Else called" 

What you wanted to do is check if it is not in the get_declared_classes() array.

Sign up to request clarification or add additional context in comments.

3 Comments

That way the __construct function(or the class) is not being called. Should I give up on this idea?
If that is the code as you're using it, then you don't need the check at all? Just start the class and run the actions.
I am running an eval("?>".$output); so it is practically re-including the same file, and if it re-includes the same file it will re-declare the same class, and if it re-declares the same class I get a fatal error xD, can you see the issue I am having with this? It has left me stumped to the point where I am questioning it's use.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.