[英]Cannot access child property in parent class (calling method inherited from parent in child object)
[英]Cannot set Parent property from child class
我有这样的视图类:
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');
}
}
但是,当我想从视图类的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";
这意味着我在视图课上,对吧?
谁能帮帮我吗?
提前致谢
您已将render()
定义为静态方法,但您正在调用它,因为它不是静态的。
我可能会从阅读这篇文章中受益: http : //chadminick.com/articles/simple-php-template-engine.html
PS你所谓的“视图”只是一个模板。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.