簡體   English   中英

無法從子類設置Parent屬性

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM