首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >php-di中的多构造函数参数注入

php-di中的多构造函数参数注入
EN

Stack Overflow用户
提问于 2017-10-07 23:14:30
回答 1查看 189关注 0票数 2

我正在尝试使用PHP-DI,但我没有完全成功。在我的简单扫描中,在Wordpress主题中的控制器需要在构造函数中注入一个PostService和一个CategoryService:

代码语言:javascript
复制
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);
    }
}

这是Index.php中容器的入口点:

代码语言:javascript
复制
require_once 'vendor/autoload.php';
require_once dirname(__FILE__).'/mvc/controllers/index_controller.php';
require_once dirname(__FILE__).'/mvc/services/categoryService.php';
require_once dirname(__FILE__).'/mvc/services/postService.php';
use DI\Container;
use DI\ContainerBuilder;

$builder = new DI\ContainerBuilder();
$builder->addDefinitions(['config.php']);
$container = $builder->build();
$indexController = $container->get('IndexController');
$indexController->Index();

以及包含定义的“config.php”:

代码语言:javascript
复制
return [
    'PostService' => \DI\object('PostService'),
    'CategoryService' => \DI\object('CategoryService'),
    'IndexController' => \DI\object()->constructor(DI\get('PostService'),DI\get('CategoryService'))
];

这是执行结果:

C:\xampp\apps\wordpress\htdocs\wp-content\themes\chester-nanalab\mvc\controllers\index_controller.php:10:对象(PostService)3005 C:\xampp\apps\wordpress\htdocs\wp-content\themes\chester-nanalab\mvc\controllers\index_controller.php:11: object(CategoryService)3006 C:\xampp\apps\wordpress\htdocs\wp-content\themes\chester-nanalab\mvc\controllers\index_controller.php:15: object(CategoryService)3006C:\xampp\apps\wordpress\htdocs\wp-content\themes\chester-nanalab\mvc\controllers\index_controller.php:16: object(CategoryService)3006

因此:

致命错误:未定义的错误:调用第19行C:\xampp\apps\wordpress\htdocs\wp-content\themes\chester-nanalab\mvc\controllers\index_controller.php中的未定义方法CategoryService::GetLastPostByCategory()

ლ(ಠ益ಠლ)╯

但是,如果我改变任务的顺序:

代码语言:javascript
复制
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);
}

我能读到:

C:\xampp\apps\wordpress\htdocs\wp-content\themes\chester-nanalab\mvc\controllers\index_controller.php:10:对象(PostService)3005 C:\xampp\apps\wordpress\htdocs\wp-content\themes\chester-nanalab\mvc\controllers\index_controller.php:11: object(CategoryService)3006 C:\xampp\apps\wordpress\htdocs\wp-content\themes\chester-nanalab\mvc\controllers\index_controller.php:17: object(PostService)3005C:\xampp\apps\wordpress\htdocs\wp-content\themes\chester-nanalab\mvc\controllers\index_controller.php:18: object(PostService)3005

(╯°-°)╯︵┻━┻?它起作用了!有人能向我解释一下发生了什么吗?

提前谢谢。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-10-08 19:03:24

问题是,您将对象属性调用为$this->$property。属性的访问方式类似于此$this->property,但定义了VISIBILITY $property;

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

代码语言:javascript
复制
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,它都是小写的魔术法

您可以获得更多关于类和对象这里的信息。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/46626006

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档