Jump to content

Recommended Posts

Hi all.

 

First off, here's my error:

Fatal error: Call to a member function fetch() on a non-object in /dash/class/clsPage.php on line 26

 

Here's the line:

public function setContent($newContent)   { $this->content = $this->S->fetch($newContent); }

 

Here's the class that contains the offending code:

class Page {
private $S;
private $content;
public function setContent($newContent)   { $this->content = $this->S->fetch($newContent); }
public function __construct($newTemplate, $newTitle, $newHeading, $newContent) {
	$this->S = new Smarty();
	$this->S->template_dir = '/dash/templates';
	$this->S->compile_dir  = '/dash/smarty/templates_c';
	$this->S->cache_dir    = '/dash/smarty/cache';
	$this->S->config_dir   = '/dash/smarty/configs';
}
}

 

Here's how I instantiate the Page class:

$P = new Page('template.intranet.tpl', APP_NAME, APP_NAME, '');

 

And that works fine. So the Page constructor instantiates the Smarty object and stores a reference to it in $S.

 

Finally, here's the line that calls the Page->setContent function:

$P->setContent('intranet.login.tpl');

 

It appears that $S is instantiated properly from within the Page constructor (Because I can set the $S variables after I instantiate it), but then the reference is lost when it leaves the constructor. fetch() is a valid, accessible function inside the Smarty class.

 

Any help is greatly appreciated.

Link to comment
https://forums.phpfreaks.com/topic/66075-solved-oo-problems/
Share on other sites

I fixed this error by moving the $this->S->fetch($newContent) statement from the setContent function into the class' __destruct function where it displays the template's content. It's a work-around, and I'm still unsure why the original code was working, but this method works very well and I'm happy with it.

Link to comment
https://forums.phpfreaks.com/topic/66075-solved-oo-problems/#findComment-331449
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.