I'm rebuilding my current code and I' m trying to do it with dependancy injection. I've downloaded Pimple and in one file, I' m trying to create few examples for myself. In doc i came to method extend, but I'm not able to make it work. For test I have created simple class:
class ExtendClass
{
private $extend = 'false';
public function extendIt()
{
$this->extend = 'true';
}
}
I've created simple object, $DI is instance of Pimple\\Container:
$DI['extend'] = function( $c )
{
return new ExtendClass();
};
I've tried to extend it with this:
$DI->extend( 'extend', function( $extend, $c )
{
$extend->extendIt();
return $extend;
} );
But it gave me this error:
Uncaught exception 'InvalidArgumentException' with message 'Identifier "extend" does not contain an object definition.
So i looked to the container nad found out, that I need to add method __invoke into my class, so i added it and make this method to return instance:
class ExtendClass
{
private $extend = 'false';
public function __invoke()
{
return $this;
}
public function extendIt()
{
$this->extend = 'true';
}
}
but after that I get this error:
RuntimeException: Cannot override frozen service "extend".
Can someone explain me what I am doing wrong? Thanks.
You need to extends service like this:
$DI->extend( 'extend', function($app)
{
$app['extend']->extendIt();
return $app['extend'];
} );
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.