Hacker News new | ask | show | jobs
by porker 4733 days ago
Dependency Injection (DI) and Dependency Injection Containers (DIC) are the "in-thing" in PHP at present, and to speak against them [0] is - like disagreeing with anything fanboys latch onto - ridiculed, without a sensible discussion or explanation.

From a testing perspective I sort-of get it; from a "this is less tightly coupled" I don't: all you've done is move the coupling to a different level. Code always has to be coupled at some level, so where's the advantage?

With the amount of noise about DI I'm sure it's something I should pay attention to rather than be turned off (as happened with Rails), but I really don't et it. Can anyone provide the missing piece in my thinking?

[0] http://till.klampaeckel.de/blog/archives/154-Dependency-Inje...

3 comments

It's interesting to follow the flow of ideas among languages/platforms.

We've repeatedly seen concepts that were popularized by the enterprise Java crowd flow down to the .NET crowd, and eventually to the PHP crowd.

IoC and DI is a good example of this. It was extremely hyped within the Java community during the early- and mid-2000s. Then this flowed into the C# community, with it getting a lot of attention during the late-2000s and early-2010s. Now it has finally made its way to the PHP community.

An astute PHP user who recognizes this flow of ideas will look at how the trend has progressed within the Java and C# communities. What they'll see is that there has be movement away from IoC and DI. The purported advantages never materialized, but there was a lot of pain and bad code left around.

Many in the Java community have moved on toward simpler frameworks and techniques, or even toward a far more static JVM-targeting language like Scala. Many C# users have similarly started moving away to other approaches.

At some point, perhaps around 2016 or so, the PHP community will come to the same conclusions about the suitability (or lack thereof) of these techniques. After experiencing the problems first hand, they'll too move back to simpler, more explicit approaches.

Interesting, I had wondered if this was the case but don't know where to look in the Java/.NET community for current best-practices (or What's Currently Hot). Where should I look?

> Many in the Java community have moved on toward simpler frameworks and techniques

What have they moved onto?

> At some point, perhaps around 2016 or so, the PHP community will

Oh so true :D

Actually, that is the point. If you decouple the objects via the container at the composition root, then you have actually achieved loose-coupling of objects.
advantage is when you manage to couple things at composition root (which is the very beginning of your application). that way there is one convenient place where coupling takes place.

read "Dependency injection in .Net" by Mark Seemann. it's the best resource to understand wtf DI is all about and what are benefits.

Yes even if you don't program .NET, the first half of the book is just about manual DI principles that could apply to almost any language. His blog also has good stuff about DI.