Jump to content

Archived

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

Cep

Call to a member function on a non-object in

Recommended Posts

Hello,

I am writing out a script from some sample code I picked up that would allow me to create template files in order to assemble pages using php variables.

In order to do this I have a class file with the following code,

[code]
<?php
class tpl {

// eval ("\$membersbit .= \" ".$tpl->get("memberslist_membersbit")."\";"); Will output a internal template
// eval("\$tpl->output(\"".$tpl->get("memberslist")."\");"); Will output an external frame template

var $templates = array();
var $templatefolder = "";

/* constuctor */
function tpl($prefix="") {
    $this->templatefolder = $prefix."templates";
}

function get($templatename, $folder = NULL) {
    if($folder==NULL) {
        $folder = "";
    }
 
    if(!isset($this->templates[$templatename])) {
        if(file_exists($this->templatefolder.$folder."/$templatename.tpl")) {
          $this->templates[$templatename]=str_replace("\"","\\\"",implode("",file($this->templatefolder.$folder."/$templatename.tpl")));
        }
    }
    return $this->templates[$templatename];
  }

  /* print template */
  function output($template) {
    headers::send();
    $template = $this->$template;
    print($template);
  }

  function str_replace($search,$replace,$text) {
    if(strstr($text,$search)) {
        $x = strpos($text,$search);
        return substr($text,0,$x).$replace.substr($text,$x+strlen($search));
    }
    else return $text;
  }
}
?>
[/code]

In my main index.php which I am going to call this class I have made a require statement at the top to the class.php file and I have then done the following,

[code]
<?php
session_start();
require "./lib/functions.php";
require "./lib/class.php";

$user = getcookie();

eval("\$tpl->output(\"".$tpl->get("test")."\");");

?>
[/code]

However I keep getting a

Fatal error: Call to a member function on a non-object in c:\inetpub\wwwroot\budgetsys\index.php on line 8

Any idea why?

Share this post


Link to post
Share on other sites
[code]
<?php
session_start();
require "./lib/functions.php";
require "./lib/class.php";

$user = getcookie();

$tpl = new tpl;
eval("\$tpl->output(\"".$tpl->get("test")."\");");

?>
[/code]

Share this post


Link to post
Share on other sites
Cheers Prismatic thought it would be something daft like that. I have a new error now but I think thats because of some missing code.

Share this post


Link to post
Share on other sites
much faster

<?php
session_start();
require_once ("./lib/functions.php");
require_once ("./lib/class.php");

$user = getcookie();

$tpl = new tpl;
eval("\$tpl->output(\"".$tpl->get("test")."\");");

?>

Share this post


Link to post
Share on other sites

×

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.