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 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 R语言卸载之后无法重装,显示电脑存在下载某些较大二进制文件行为,怎么办
  • ¥15 java 的protected权限 ,问题在注释里