Dependency Injection (And container?)
Posted: Tue Feb 19, 2013 11:35 pm
Hello!
I am migrating a legacy code base to something more testable. To level set, we have about a dozen classes that are fairly close to being testable (I think?), but we have around 140 functions that have not yet been migrated to become classes.
One of the issues we have run into is that the majority of the code passes around the $db object, which is in the global scope (yuck!).
I've read with interest here that this sort of thing is best resolved with Dependency injection. I've started down that path, and have manual DI for most, with the $db object being a parameter for the calls.
Now I want to go the next step, and get those files testable. I understand that the way to do that should be to use the DI container. My project has composer, so I am leaning towards Pimple, which is a really simple DI container.
But before I start hacking on that, are my assumptions/understandings correct? Is using a DI container going to make it easier to test my units of code? With the DI container, can I also move other objects (like configuration values) into the container, so we just need to pass the container into the units of code, and get both DB & configs?
Any guidance is greatly appreciated.
I am migrating a legacy code base to something more testable. To level set, we have about a dozen classes that are fairly close to being testable (I think?), but we have around 140 functions that have not yet been migrated to become classes.
One of the issues we have run into is that the majority of the code passes around the $db object, which is in the global scope (yuck!).
I've read with interest here that this sort of thing is best resolved with Dependency injection. I've started down that path, and have manual DI for most, with the $db object being a parameter for the calls.
Now I want to go the next step, and get those files testable. I understand that the way to do that should be to use the DI container. My project has composer, so I am leaning towards Pimple, which is a really simple DI container.
But before I start hacking on that, are my assumptions/understandings correct? Is using a DI container going to make it easier to test my units of code? With the DI container, can I also move other objects (like configuration values) into the container, so we just need to pass the container into the units of code, and get both DB & configs?
Any guidance is greatly appreciated.