TL; DR , because defining dependencies as closures allows the dependency injection container to create them on demand, so you donโt have to worry about how to define them and manually manage their dependencies.
Pimple , , .
, Pimple , , :
$container['sample-param'] = 'foo';
echo $container['sample-param'];
, sample-param - , , . :
$container['myService'] = function($c) {
$service = new \Some\Complicated\Service();
$service->setCache($c['cache']);
return $service;
}
$container['cache'] = function($c) {
$cache = new \Cache\Driver();
$cache->setDbConnection($c['db']);
return $cache;
}
$container['db'] = function($c) {
$dbConnection = new \Database\Connection($c['db-config']);
return $dbConnection;
}
$container['db-config'] = [
'username' => 'foo',
'password' => 'bar',
'host' => 'localhost'
];
$container['myService']->doSomething();
, .
myService cache, myService. Pimple , , Pimple . myService, Pimple , myService , , . , Pimple ($ c - Pimple) dependecies ( cache). Pimple , , ..... , , , db-config, . .
, , ? , myService , . , , , , . myService , cache, .