Dependency Hell — how does one pass dependencies to deeply nested objects?
Solution 1:
It's a common misconception that dependencies need to be passed through the object graph. To summarize the example Miško Hevery gives in Clean Code: Don't look for things, a House that needs a Door, doesnt need to know about the Lock in the Door:
class HouseBuilder
{
public function buildHouse()
{
$lock = new Lock;
$door = new Door($lock);
$house = new House($door);
return $house;
}
}
As you can see, House is completely oblivious of the fact that the Door in it requires a lock. It's the responsibility of the HouseBuilder to create all the required dependencies and stack them together as they are needed. From the inside out.
Consequently, in your scenario you have to identify which objects should operate on which dependencies (cf Law of Demeter). Your Builder then has to create all collaborators and make sure the dependencies are injected into the appropriate objects.
Also see How to Think About the “new” Operator with Respect to Unit Testing
Solution 2:
If you are stumbling upon the same question check Hervey's article that hits the bulls eye
http://misko.hevery.com/2008/10/21/dependency-injection-myth-reference-passing/
In case the article vanishes in future here is the excerpt
"Every object simply knows about the objects it directly interacts with. There is no passing of objects reference just to get them into the right location where they are needed."
So what one need to do is instead of creating a deep nested object graph with passing the dependencies fro top to bottom, go horizontal and manage dependencies elsewhere.