Am trying to call an object's method inside of an included file which the included file itself, was loaded in the class with output buffer like below:
public function render($file){
ob_start();
require_once($file);
$this->template = ob_get_contents();
ob_end_clean();
return $this->template;
}
Getting these errors:
Notice: Undefined variable: template
Fatal error: Call to a member function showTitle() on a non-object
The included file is to be used as a template and there will be many. It works fine when used as static
property and method set, but not with the instantiated object's property and method set which is the required way.
Below are all test files for your review.
Class file "classTest.php":
<?php
// classTest.php
class Test{
protected $template;
protected $title = "Title";
static $titleStatic = "Title Static";
public function render($file){
ob_start();
require_once($file);
$this->template = ob_get_contents();
ob_end_clean();
return $this->template;
}
public function showTitle(){
return $this->title;
}
static function showTitleStatic(){
return self::$titleStatic;
}
}
?>
Test file "test.php":
<?php
// test.php
require_once 'classTest.php';
$template = new Test;
echo $template->showTitle();
$templateFile = 'includeTest.php';
echo $template->render($templateFile);
?>
Include file "includeTest.php":
<!-- includeTest.php -->
<div id="container">
<div id="titleStatic"><?php echo Test::showTitleStatic(); ?></div>
<div id="title"><?php echo $template->showTitle(); ?></div>
</div>