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]
(╯°□°)╯︵ ┻━┻? It works! Can anyone explain to me what is happening?
Thanks in advance.