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

Multiple constructor parameter injection in php-di

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

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:

中容器的入口点
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':

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: 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]

等等:

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

ლ(ಠ益ಠლ)╯

但是,如果我更改作业的顺序:

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: 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]

(╯°□°)╯︵ ┻━┻?有用! 谁能给我解释一下这是怎么回事?

提前致谢。

问题是您将对象 属性 称为 $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.

请记住,任何 magic method 都是小写的 __construct

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