beredon Posted August 21, 2007 Share Posted August 21, 2007 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 More sharing options...
beredon Posted August 23, 2007 Author Share Posted August 23, 2007 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 More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.