我有这样的观景课:
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();
}
}然后,我从控制器子类中设置视图属性,如下所示:
class Index extends Controller {
function __construct() {
parent::__construct();
$this->view->js = "test";
}
public function index() {
$this->view->render('index/index');
}
}但是,当我想从"header.php“获得$ this ->js (在视图类上设置为render function )时,我总是收到以下错误消息:
Fatal error: Uncaught Error: Using $this when not in object context我被试着检查,我在正确的课堂上吗?在"header.php“文件中使用此方法:
echo get_class(); // and this method return "View";这意味着我上过观景课,对吧?
有人能帮帮我吗?
提前感谢
发布于 2017-05-13 13:50:59
您已经将render()定义为一个静态方法,但是您正在调用它,因为它不是静态的。
我可能会从阅读以下文章中获益:http://chadminick.com/articles/simple-php-template-engine.html
P.S. --您称之为“视图”--只是一个模板。
https://stackoverflow.com/questions/43952912
复制相似问题