We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

Di bound to this on closures not working on 3.0

if I var_dump($this->get("config")) it shows me the config instance, but if I do var_dump($this->config) it throws the notice Undefined property: Phalcon\Di\FactoryDefault::$config in ... what's happening?

edited Aug '16

I can't find any documentation pointing that I can access to the services as properties. The documentation says to use "get", "getServiceName" or array notation... but I thought that it was possible to use it as in MVC\User\Components or Plugins.

According to the blog post, it should be working... https://blog.phalcon.io/

Phalcon\Di is now bound to services closures allowing use Phalcon\Di as $this to access services within them. Additionally, closures used as handlers in Mvc\Micro are now bound to the $app instance

Old way:

$diContainer->setShared(
    'modelsCache',
    function () use ($config) {
        $frontend = '\Phalcon\Cache\Frontend\\' . $config->get('modelsCache')->frontend;
        $frontend = new $frontend(
            [
                'lifetime' => $config->get('modelsCache')->lifetime,
            ]
        );
        $config   = $config->get('modelsCache')->toArray();
        $backend  = '\Phalcon\Cache\Backend\\' . $config['backend'];

        return new $backend($frontend, $config);
    }
);

New way:

$diContainer->setShared(
    'modelsCache',
    function () {
        $frontend = '\Phalcon\Cache\Frontend\\' . $this->config->get('modelsCache')->frontend;
        $frontend = new $frontend(
            [
                'lifetime' => $this->config->get('modelsCache')->lifetime,
            ]
        );
        $config   = $this->config->get('modelsCache')->toArray();
        $backend  = '\Phalcon\Cache\Backend\\' . $config['backend'];

        return new $backend($frontend, $config);
    }
);

Well actually - in Di class scope - you can't access them as property beacause they are never added as property and we couldn't solve this with __get neither beacause in class scope it's never called. That would mean that properties need to be added to di directly - not sure if we want to do it - im guessing not.