I’ve been reading up upon Dependency Injection and DI Containers. However I can’t wrap my head around this concept.
How would the DI container know that ‘controller’ depends on ‘loader’ and load ‘loader’ before ‘controller’?
The way I see it is that it would need some kind of config file to keep track of the dependencies, and if so isn’t it just easier to write:
// Controller
function __construct() {
$this->load = new \Framework\Core\Loader;
}
Yes, DICs need upfront Configuration, either in some config file or through stacking together factory closures or by annotating your source code.
Yes, it is easier to create instances in the ctor, but that will eliminate all the benefits of DI, because you are not injecting the dependencies anymore.
Also note that you dont need a DIC in order to do DI. DI is simply the act of injecting dependencies in your code through constructors, setters or the using methods.