Install with Composer:
composer require drhino/container:^3.0.0
class Dependency
{
public string $var = '';
}
class Init
{
public function __construct(Dependency $dependency, String $value)
{
$dependency->var = $value;
}
}
$container = new drhino\Container\Container;
$container
->add(Init::class, [
// The arguments of the constructor
'dependency' => $container->ref(Dependency::class),
'value' => 'Hello world',
])
->add(Dependency::class)
;
Use $container->ref() to reference an identifier before it has been added into the container.
// Executes __construct()
$init = $container->get(Init::class);
// Prints 'Hello world'
echo $container->get(Dependency::class)->var;
The following are exactly the same:
$container->add(Dependency::class);
$container->add($id = Dependency::class, $resource = Dependency::class, $arguments = []);
$container->add($id = Dependency::class, $arguments = []);