i have simple app and i'm trying to start using DI container. I downloaded Pimple, studied the code and documentation. After a while i have come to funny thing. I have two classes, which in special cases communicate between themselves.
class Foo
{
/**
* @var Bar
*/
private $bar;
public function __construct(Bar $bar)
{
$this->bar = $bar;
}
}
class Bar
{
/**
* @var Foo
*/
private $foo;
public function __construct(Foo $foo)
{
$this->foo = $foo;
}
}
I have defined Pimple container, with two classes, like this:
$container = new Pimple\Container();
$container['foo'] = function($c)
{
return new Foo($c['bar']);
};
$container['bar'] = function($c)
{
return new Bar($c['foo']);
};
Here is var_dumped container:
object(Pimple\Container)[14]
private 'values' =>
array (size=2)
'foo' =>
object(Closure)[17]
'bar' =>
object(Closure)[18]
private 'factories' =>
object(SplObjectStorage)[15]
private 'protected' =>
object(SplObjectStorage)[16]
private 'frozen' =>
array (size=0)
empty
private 'raw' =>
array (size=0)
empty
private 'keys' =>
array (size=2)
'foo' => boolean true
'bar' => boolean true
The problem is, when i create use $container['foo'], it try to inject class Bar, which require foo, so there's error with Maximum nesting. My idea is, why there is not passed closure? Which will be executed when it will be really needed in class?
Or did I complettly misunderstood what closure is?