2

In previous applications I've worked on, I've had a single factory class which built my object structure, and all dependencies were passed to each class through its constructor parameters.

I'm now considering using a DIC framework such as Pimple, in which case it seems like you have a global dependency container which would be referenced inside a class' constructor to instantiate its dependency fields.

Am I missing any other viable approaches to doing dependency inversion? What are the pros and cons to each approach? When would you want to use one approach above another?

  • What you are missing is looking at the documentation and maybe taking the container for a test drive before deciding how it works. How in the world did you manage to come up with the notion that the pimple container is referenced inside of constructors? Strange to say the least. – Cerad Sep 27 '16 at 1:30
  • @Cerad I did scan the one page of docs they provide, but probably do need to spend a little more time studying their examples. I believe my misconception was based on reading some other SO questions relating to Pimple. Do note, though, that my question isn't strictly about Pimple. I'd really like a more high-level analysis of different approaches to dependency inversion and the pros / cons of each. – Nathan Arthur Sep 27 '16 at 15:58
  • I don't think you understand what Dependency Inversion is. Your question is solely about Dependency Injection. Rejecting edits does not change that fact. – user318054 Mar 16 '17 at 19:37
  • @fnt While I do mention dependency injection, my intention in asking this question was to discover other ways to do dependency inversion, other than dependency injection, of which I wasn't aware. Specifically: "Am I missing any other viable approaches to doing dependency inversion? What are the pros and cons to each approach? When would you want to use one approach above another?" – Nathan Arthur Mar 16 '17 at 21:29
  • @fnt If I really don't understand dependency inversion, and given that my question was for the purpose of learning more about dependency inversion, I would suggest your time would be better spent explaining this in an answer rather than editing my question to remove the topic I wanted to learn more about. – Nathan Arthur Mar 16 '17 at 21:30
2

Pimple documentation shows how to use it to inject dependencies into your objects constructors, which is definitely the way to use it.

Yes, you have a global dependency container, and no, you don't call it inside a class' constructor.

It replaces your factory class, you ask it to create some top-level object, and it takes care of creating all dependencies recursively according to the rules you gave it.

  • Ok... This makes more sense. But then I'm not sure what its advantage is. I still have to define my entire object model within a file--it's just in Pimple instead of my factory. Any insight on what advantages Pimple or another dependency injection container framework actually offer? – Nathan Arthur Sep 26 '16 at 18:13
  • 1
    I don't use Pimple nor PHP, so I don't know it's exact capabilities. The configuration can be split into several files (per module, etc.). Also, it is generally easier to maintain, and tends to better cover special cases. – Philippe Sep 26 '16 at 18:40
1

It seems to me that Pimple is pretty strictly inferior. You have a much tighter coupling to the DI container, and a bunch of hardcoded string keys everywhere that cannot be changed. The constructor parameter approach is much superior because this has zero coupling to the container. For instance, imagine how much fun it would be to write a test using a class with a Pimple container compared to the constructor-arguments approach.

  • +1. When in doubt, try writing a unit test for both methods and see which makes more sense. – Daenyth Sep 26 '16 at 17:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.