PHP-DI Register Instance for ctor Injection -


i'm using php-di , using twig in project. i'd register instance of $twig php-di instance injected ctor argument on objects it's needed. i'd use php definitions , avoid phpdoc annotations, , have read http://php-di.org/doc/php-definitions.html

here's basic example:

$builder = new containerbuilder(); $builder->adddefinitions(['twig_environment' => $twig]); $container = $builder->builddevcontainer(); 

then have $twig ctor argument in other classes. possible? clear, don't want have create definition each object uses $twig.

public function __construct(\twig_environment $twig) {     $twig->render('homepage.twig'); } 

the error i'm getting indicates php-di trying create new instance of twig_environment instead of using instance created.

this correct, except method call create container:

$container = $builder->builddevcontainer(); 

that creates entirely new container , ignores have configured above ;) (it's static method can use create new container this: $container = containerbuilder::builddevcontainer();).

you should use instead:

$container = $builder->build(); 

Comments

Popular posts from this blog

c - Bitwise operation with (signed) enum value -

xslt - Unnest parent nodes by child node -

YouTubePlayerFragment cannot be cast to android.support.v4.app.Fragment -