1

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;
}
user555
  • 1,564
  • 2
  • 15
  • 29
  • By the way, it's even easier (and obvious) to write `function __construct($loader) { $this->load = new $loader; }`. – Samy Dindane Jun 29 '12 at 21:38

2 Answers2

2

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.

Gordon
  • 312,688
  • 75
  • 539
  • 559
  • So if I would want to do DI I would inject the Loader class into the Controller, eg. `$controller = new Controller($loader);` then in controller `function __construct($injected) { $this->load = $injected; }`? – user555 Jun 29 '12 at 21:45
  • @user Yes. You simply inject all the dependencies to put the object into valid state through the constructor. Grasp that concept first before messing with DICs. For most applications, you dont need them anyway. – Gordon Jun 29 '12 at 21:48
  • How would I go along to load a class. Should I create a factory to create the new class and insert all the needed dependencies? – user555 Jun 29 '12 at 21:49
  • @user1320555 Yes, Factories do the job quite well. See http://stackoverflow.com/questions/6094744/dependecy-hell-how-does-one-pass-dependencies-to-deeply-nested-objects/6095002#6095002 – Gordon Jun 29 '12 at 21:50
  • @Gordon I have come across this issue recently (just learning too) and have come to certain solutions. Please be kind to take a look: http://stackoverflow.com/questions/21368535/dependency-injection-and-initialization-of-the-object – Ilia Ross Jan 29 '14 at 18:55
  • @IliaRostovtsev not sure what you want to say about it. – Gordon Jan 29 '14 at 20:03
  • @Gordon Well, I just wanted to know, if using Dice from your point of view is alright. What it's obvious limitations (if any). I haven't seen DIC that would load dependencies with so little of code (probably because I'm yet not very experienced). Are there well known DIC that would you would prefer over that one for some reasons? Or everything is strictly depends on the project? Thanks for formatting my question! ;) – Ilia Ross Jan 30 '14 at 07:03
  • @IliaRostovtsev I never used Dice before. I only know the Symfony DI and RG Injektor. Both would allow you the same easy way of getting service, given that you configured the services. I have also worked with a DIC that was basically just a giant handcrafted factory with methods calling other methods to return a service. It really depends on what you need. It wasn't pretty code but it worked. For small projects DICs are overkill imo because you can easily use a Builder or dedicated Factories. – Gordon Jan 30 '14 at 07:54
  • @Gordon Thank you, Gordon! I will also take a look at *RG Injektor*, never heard of this one! Yes, the code wan't beautiful and I already wrote to the author offering to format it properly and add comments! – Ilia Ross Jan 30 '14 at 08:03
0

Just to complete what Gordon said:

Yes, DICs need upfront Configuration, either in some config file or through stacking together factory closures or by annotating your source code.

The easiest/quickest way here is annotations, check out this example:

class Loader {
    //...
}

class MyController {
    /**
     * @Inject
     * @var Loader
     */
    private $loader;

    public indexAction() {
        // You can use $this->loader
    }

}

In this example, the dependency (the loader) has been injected through annotations: the code is clear, readable, and you don't need to write any configuration file. This is IMO the simplest solution.

If you are interested in this approach, I took the example from PHP-DI, a dependency injection framework working with annotations (on which I work).

Matthieu Napoli
  • 48,448
  • 45
  • 173
  • 261