php - Pimple - extend object definition -
i'm rebuilding current code , i' m trying dependancy injection. i've downloaded pimple , in 1 file, i' m trying create few examples myself. in doc came method extend, i'm not able create work. test have created simple class:
class extendclass { private $extend = 'false'; public function extendit() { $this->extend = 'true'; } } i've created simple object, $di instance of pimple\container:
$di['extend'] = function( $c ) { homecoming new extendclass(); }; i've tried extend this:
$di->extend( 'extend', function( $extend, $c ) { $extend->extendit(); homecoming $extend; } ); but gave me error:
uncaught exception 'invalidargumentexception' message 'identifier "extend" not contain object definition.
so looked container nad found out, need add together method __invoke class, added , create method homecoming instance:
class extendclass { private $extend = 'false'; public function __invoke() { homecoming $this; } public function extendit() { $this->extend = 'true'; } } but after error:
runtimeexception: cannot override frozen service "extend".
can explain me doing wrong? thanks.
you need extends service this:
$di->extend( 'extend', function($app) { $app['extend']->extendit(); homecoming $app['extend']; } ); php dependency-injection pimple
No comments:
Post a Comment