Strictly speaking, your problem is not really pimple related.
You simply have a circular constructor dependency, there is no way to fix those. A needs B needs A needs B...
The thing is, usually, two classes that depend on each other do not make much sense. But in the rare case where that do happen, one of the two should be a lighter dependency, where you inject the object through a setter or some mechanism like that after instantiation.
By passing the container around and instantiating inside ClassB
, you are hiding your dependencies, which beats the purpose of having a dependency injection container. Now ClassB
depends on Container
, not on ClassA
, which is what you wanted in the first place.
Instead of doing that, just add a method setA(Class A $a)
to your ClassB
.
Then, you inject your dependency by calling $b->setA($container['ClassA']);
when you actually need it, not on the DI container. As Adam points out, you can even do the setter injection in your container by extending your services and using the setters on the service definition.
But again, to reiterate, your main problem is to have a circular dependency. Re-think that. It's very likely a sign your design could do with some improvements.