dsvjw20866 2017-10-07 23:14
浏览 64
已采纳

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

I'm trying to use PHP-DI, but I did not quite succeed. In my simple scanario, a controller, in a Wordpress Theme, needs a PostService and a CategoryService injected in constructor:

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);
    }
}

This is my entry point for the container in the Index.php:

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();

And the 'config.php' which contains the definitions:

return [
    'PostService' => \DI\object('PostService'),
    'CategoryService' => \DI\object('CategoryService'),
    'IndexController' => \DI\object()->constructor(DI\get('PostService'),DI\get('CategoryService'))
];

This is the execution result:

C:\xampp\apps\wordpress\htdocs\wp-content\themes\chester-nanalab\mvc\controllers\index_controller.php:10: object(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)[3006] C:\xampp\apps\wordpress\htdocs\wp-content\themes\chester-nanalab\mvc\controllers\index_controller.php:16: object(CategoryService)[3006]

and so:

Fatal error: Uncaught Error: Call to undefined method CategoryService::GetLastPostByCategory() in C:\xampp\apps\wordpress\htdocs\wp-content\themes\chester-nanalab\mvc\controllers\index_controller.php on line 19

ლ(ಠ益ಠლ)╯

but, if I change the order of the assignments:

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);
}

I can read:

C:\xampp\apps\wordpress\htdocs\wp-content\themes\chester-nanalab\mvc\controllers\index_controller.php:10: object(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)[3005] C:\xampp\apps\wordpress\htdocs\wp-content\themes\chester-nanalab\mvc\controllers\index_controller.php:18: object(PostService)[3005]

enter image description here

(╯°□°)╯︵ ┻━┻? It works! Can anyone explain to me what is happening?

Thanks in advance.

  • 写回答

1条回答 默认 最新

  • dqyl2374 2017-10-08 19:03
    关注

    The problem is that you are calling an object property as $this->$property. Properties are accessed like this $this->property but defined VISIBILITY $property;

    So, you should change your code to this

    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);
        }
    }
    

    For parent it is different, because you are using static accessor (you are not getting the property as static, but it is the way to do it) parent::$property.

    And remember, it is __construct in lowercase for any magic method.

    You can have more info for Classes and Objects here.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?