In the guide we can see this example: http://www.yiiframework.com/doc-2.0/yii-di-container.html
In the example, dependency declaration and object creation are happening at the same place in code. I am trying to move the declaration to one spot. I want to declare all dependencies in one place, and then just use them throughout my code. I have some problem, and I do not know why it is happening.
For example. I have interface SomeServiceInterface, and implementation SomeService. I would like to declare the dependency in one place in application like this:
$container->set('common\components\SomeServiceInterface', [
'class' => 'common\components\SomeService',
]);
And then, when I need the object of SomeService ( actually I need the implementation of that interface ), I would like to do this
$container = new Container;
$someService = $container->get('common\components\SomeServiceInterface');
It seems that this is possible only if you declare and use the dependencies at the same spot in code, like in the guide.
What I have tried, is that I create component that is bootstraped at the application start, and inside that componenet I have declared all my dependencies. Then in my controller, I have tried to use one of those, but I am getting an error.
Code example.
Component:
class ContainerConfigurator extends Component
{
/**
* interface => implementation
*
* @var [type]
*/
private $_config = [
'common\components\SomeServiceInterface' => 'common\components\SomeService'
];
public function init()
{
$container = new Container;
foreach ($this->_config as $interface => $implementation) {
$container->set($interface, $implementation);
}
}
}
Bootstraped:
'bootstrap' => ['common\components\ContainerConfigurator'],
Used:
use yii\di\Container;
$container = new Container;
$service = $container->get('common\components\SomeServiceInterface');
Error:
Not instantiable – yii\di\NotInstantiableException
Can not instantiate common\components\SomeService.
Is this even possible, or am I doing something wrong ?