2017-05-13 125 views
1

設置Parent屬性我有鑑於這樣的類:無法從子類

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」獲得$這個 - > JS我總是得到這個錯誤信息:

Fatal error: Uncaught Error: Using $this when not in object context 

我被試圖檢查,我在正確的類嗎?使用這種方法「的header.php」文件:

echo get_class(); // and this method return "View"; 

,這意味着我在視圖類的吧?

任何人都可以幫我嗎?

在此先感謝

回答

0

您已經定義render()爲靜態方法,但是你調用它,因爲它不是一成不變的。

我可能會從閱讀受益:http://chadminick.com/articles/simple-php-template-engine.html

附:你稱之爲「視圖」只是一個模板。

+0

哦......男人。我不知道,如果我的渲染功能是靜態的。現在是工作。非常感謝你@tereško。 – bagongpct