我有这样的视图类:
class View {
public function __construct() {
}
public static function render($name) {
require 'views/user/header.php';
require 'views/user/'.$name.'.php';
require 'views/user/footer.php';
}
}
我在控制器中调用视图类,如下所示:
class Controller {
function __construct() {
$this->view = new View();
}
}
然后我从控制器子类中设置了view属性,如下所示:
class Index extends Controller {
function __construct() {
parent::__construct();
$this->view->js = "test";
}
public function index() {
$this->view->render('index/index');
}
}
但是当我想从视图类的render函数设置的“header.php”获取$ this-> js时,我总是收到此错误消息:
Fatal error: Uncaught Error: Using $this when not in object context
我试图检查,我是否在正确的班级?在“header.php”文件中使用此方法:
echo get_class(); // and this method return "View";
这意味着我在视图课上,对吗?
有人可以帮助我吗?
提前致谢
答案 0 :(得分:0)
您已将render()
定义为静态方法,但您正在调用它,因为它不是静态的。
我可能会从阅读此内容中受益:http://chadminick.com/articles/simple-php-template-engine.html
P.S。您所谓的“视图”只是一个模板。