phpphp-di

Multiple constructor parameter injection in 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.


Solution

  • 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.