PHP how can I return the calling function (to avoid the scope of the function)

advertisements

I have two files:

template.php:

<div><?php echo $myVar; ?></div>

controller

class MyClass extends Base {
    public function view() {
        $myVar = 'hello world';
        $this->loadTemplate('main');
    }
}
abstract class Base {
    public function loadTemplate($template) {
        require_once("templates/$template.php");
    }
}

but this must not works because the require is in scope of loadTemplate function, how can i return the call to require function in the loadTemplate? i want the require be included in the view() scope using single function like $this->loadTemplate(); and not using require_once($this->getTemplatePath())

any idea?


You can't. Generally what you're trying to do is accomplished by passing an array with your variables ($this->loadTemplate(array('myVar' => 'hello world'))) and then calling extract($vars) in the loadTemplate method.