$container['session'] = $container->share(function ($c) {
return new Session($c['session_storage']);
});
$dic->setClass('session', function() use ($dic) {
return new Session($dic->getObject('session_storage'));
});
$obj = $dic->getObject('session');
Jelmer Schreuder wrote on Thursday 2nd of August 2012:Hadn't considered implementing arrayaccess for the DiC yet, it's a good idea. If you make a feature request on Github I'll implement it over the weekend.
$dic->setClass('Foo', function() use ($dic) {
return new Foo("bar");
});
$dic->setClass("bar", "bar");
$dic->setClass('Foo', function() use ($dic) {
return new Foo($dic->getClass("bar"));
});
$dic->setClass('Foo', function($param) use ($dic) {
return new Foo($param);
});
$dic->setObject('Foo', 'bar', $dic->forge('Foo', 'bar'));
Jelmer Schreuder wrote on Tuesday 7th of August 2012:[edit:] But just in case and to clarify: the DiC is no replacement for configuration. It's meant to be able to generate objects and keep a registry of those. Keeping a collection of values is not a functionality that belongs in the DiC, it's something you'd use configuration for.
$di = new Zend\Di\DependencyInjector;
$di->getInstanceManager()->setParameters('Foo', array(
'bar' => 'bar'
);
$foo = $di->get('Foo');
$dic->forge(array('Foo', 'foo'), 'bar');
$dic->setClass('Baz', function() use ($dic) {
return new Baz($dic->getObject('Foo', 'foo'));
});
$dic->setClasses(array(
'Foo' => function($param = 'bar') {
return new Foo($param);
},
'Baz' => function() use ($dic) {
return new Baz($dic->forge('Foo'));
},
));
It looks like you're new here. If you want to get involved, click one of these buttons!