I have dependency and definition settings when setting up a container using ContainerBuilder, and then compiling it to get the actual one Container, but whenever I try to nest dependencies, they are always ignored.
Am I missing the concept of a method injectOn(), or am I doing something wrong here ( $this->translatorremains unassigned)? I tried different approaches to this, both creating an instance of the class, and adding an object to ContainerBuilder, and also passing it as a definition \DI\object(), as with the same result.
<?php
include "../vendor/autoload.php";
class Translator
{}
class TextHandle
{
protected $translator;
public function setTranslator (Translator $translator)
{
$this->translator = $translator;
}
public function test ()
{
var_dump($this->translator);
}
}
class TestCase
{
protected $translator;
public function __construct (Translator $translator)
{
$this->translator = $translator;
}
}
$containerBuilder = new \DI\ContainerBuilder();
$containerBuilder->addDefinitions([
Translator::class => \DI\object(),
]);
$container = $containerBuilder->build();
$textHandle = new TextHandle();
$container->injectOn($textHandle);
$textHandle->test();
$translator = $container->get(Translator::class);
var_dump($translator);
$textHandle = $container->make(TextHandle::class);
$textHandle->test();
$testCase = $container->make(TestCase::class);
var_dump($testCase);
source
share