cphalcon icon indicating copy to clipboard operation
cphalcon copied to clipboard

Cannot replace DI-injected service

Open deadkrolik opened this issue 12 years ago • 1 comments

For tests I'm trying to replace injected service. But can't do it.

<?php
error_reporting(E_ALL | E_NOTICE | E_STRICT);

$di = new \Phalcon\DI\FactoryDefault();
$di->set('inst', '\\Instance1');

class Instance1 {}
class Instance2 {}

$di->set('view', function() {
    return new \Phalcon\Mvc\View();
});

class IndexController extends \Phalcon\Mvc\Controller
{
    public function indexAction()
    {
        echo get_class($this->inst) . '<br />'; //outputs "Instance1"

        //trying to replace
        $this->getDi()->remove('inst');
        $this->getDi()->set('inst', '\\Instance2');

        echo get_class($this->inst) . '<br />'; //still outputs "Instance1"
    }
}

$application = new \Phalcon\Mvc\Application();
$application->setDI($di);
echo $application->handle()->getContent();

Is there a right way to replace a service?

deadkrolik avatar Jul 18 '14 10:07 deadkrolik

For questions like this you can use the forum. http://forum.phalconphp.com/

Thanks

ghost avatar Jul 21 '14 02:07 ghost