Parent Delegation
Most container methods will look to the parent container for entries it might not have.
get
– If the current container doesn't have the provided name, it delegates to the parenthas
– If the current container doesn't have the provided name, it delegates to the parentcontainsType
– If the current container doesn't have the provided type, it delegates to the parentgetByType
– First, all entries of the provided type from the parent are collected, and then any entries in the current container are added (if it so happens that entries have the same name in the parent, they are overwritten by the child)getFirst
– If the current container doesn't have the provided type, it delegates to the parentgetNames
– This method does not delegategetParent
– Obviously, this method returns the parent if presentgetType
– If the current container doesn't have the provided name, it delegates to the parentnamed
– If the current container doesn't have the provided name, it delegates to the parent
Aside from system resources, there is no limit to the number of parents a container can have.
$props = new \Caridea\Container\Properties([
'db.host' => 'example.com',
'db.port' => 1337,
'db.user' => 'dba',
]);
$backend = \Caridea\Container\Objects::builder()
->lazy('db', 'My\Db\Service', function ($c) {
return new \My\Db\Service(
$c->get('db.host'),
$c->get('db.port'),
$c->get('db.user')
);
})->build($props);
$frontend = \Caridea\Container\Objects::builder()
->lazy('controller', 'My\Web\Controller', function ($c) {
return new \My\Web\Controller(
$c->get('db')
);
})->build($backend);
$controller = $frontend->get('controller');