Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ioc Containers and dynamic languages (take 2)

I've been reading up a lot on Dependency Injection, Inversion of Control, and IoC containers. I also primarily program in dynamic languages (PHP at work, Python at home). Here are the things I'm finding, but this leaves a lot of gaps for me to fill as I piece it all together:

  • Why are IOC containers unnecessary with dynamic languages?
  • Why do I need an IoC container as opposed to straightforward DI code?
  • Advantages an IoC container provides beyond simple DI (blog post)

So what I'm reading is: IoC containers are a much bigger deal in static languages, because it's so much easier to perform DI in dynamic languages. But they also provide benefits far beyond DI, like managing dependencies for you and saving you from having to string together a dozen objects by hand. And, incidentally, they are complicated, so don't try to do them yourself (but no good ones exist for PHP).

I feel like this information leaves me kind of... stuck. What do I do with it? I work in a very large codebase, with very complicated dependencies (and probably a strong need for refactoring, but that's another parallel issue). We've done very poorly at implementing DI up till now, and I'm really trying to turn us in the right direction. There just seems to be nothing out there in regards to dynamic languages and IoC (or at least IoC containers).

Am I better off "hand-stringing" dependencies together for the time being, and worry about automating it in a container later, after I get a better handle on the principles? Is it worth implementing my own simple IoC container? Or is the benefit just not ultimately worth the cost in PHP?

like image 576
keithjgrant Avatar asked Oct 13 '22 17:10

keithjgrant


1 Answers

For PHP try Symfony Dependency Injection. It's (supposedly - I've too little experience to validate that) based on how Java Spring works, but using a lot of PHP 'magic' too. AS a result it's quite lightweight and easy to use, while being able to do quite a lot.

like image 129
Mchl Avatar answered Nov 01 '22 17:11

Mchl