Reputation: 9518
I'm building a reusable library in PHP which will be used by multiple in-house applications (some web some not). I use constructor dependency injection mostly for my classes, for example injecting my Repository classes into business logic classes:
namespace \Company\Project\BusinessLogic;
class MyComplexBusinessWorkflow
{
private $_repository;
public function __construct(IMyEntityRepository $repository)
{
$this->_repository = $repository
}
...
}
So with Dice I'd like to have something like this which inject default for the library IMyEntityRepository
implementation:
$my_complex_workflow_instance = $dice->create('MyComplexBusinessWorkflow');
instead of:
$my_complex_workflow_instance = new MyComplexBusinessWorkflow(new MyEntityMySQLRepository(new MysqlConnectionWrapper()));
I'm ok with embedding particular DI container library into my library code, but not sure how to manage container instance ($dice
in the code above) itself.
Upvotes: 2
Views: 855
Reputation: 9518
Didn't get an answer, so provide my "solution". I've implemented a separate Builder class for instantiating DI container.
Code that will use a library will have a dependency on the particular container, this can be absracted by an interface. Unfortunatelly different DI containers work in different way, so that's not a complete abstraction.
In comments I was pointed to this interesting initiative to unify DI containers in PHP: https://github.com/container-interop/container-interop
Upvotes: 1