2
4

1 回答 1

3

问题是您将对象属性称为$this->$property. 像这样访问属性$this->property但已定义VISIBILITY $property;

因此,您应该将代码更改为此

class IndexController extends ChesterBaseController {
    private $_postservice;
    private $_categoryService;
    public function __construct(PostService $postservice, CategoryService $categoryService){
      var_dump($postservice);
      var_dump($categoryService);
      parent::__construct();
      $this->_categoryService = $categoryService;
      $this->_postservice = $postservice;
      var_dump($this->_postservice);
      var_dump($this->_categoryService);

    }
    public function Index(){
        $firstRowPost = $this->_postservice->GetLastPostByCategory('video');
       // ...
        echo $this->renderPage('index', $vm);
    }
}

因为parent它是不同的,因为您正在使用static访问器(您没有将属性设为静态,但它是这样做的方式)parent::$property

请记住,对于任何魔法方法,它都是__construct小写的。

您可以在此处获得有关类和对象的更多信息。

于 2017-10-08T19:03:24.500 回答