2017-05-13 127 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