In this example I have classA and classB that I am using with pimple container.
They both have a dependency on each other. However when setting this up with pimple DIC, the below code causes an infinite loop...
There must be a way of doing this in pimple but I can't see it in the docs... Any ideas how to prevent the infinite loop?
// PIMPLE CONTAINER
use Pimple\Container;
$container = new Container();
use Classes\ClassA;
$container['ClassA'] = function ($c) {
return new ClassA($c['ClassB']);
};
use Classes\ClassB;
$container['ClassB'] = function ($c) {
return new ClassB($c['ClassA']);
};
Instead of instantiating ClassA from classB, pass the container to ClassB. Then when you need ClassA within ClassB, you can use the container you have passed to it to initiate / get instance ot ClassA.