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

How to use the definition of the DI before the first

$di->setShared('logger', function () use ($config) {

return new FileAdapter($config->logger->application);

});

$di->setShared('cache', function () use ($config) {

$cache = new Redis();
$res = $cache->connect($config->redis->host,$config->redis->port,$config->redis->lifetime);
//if($res !== true){
   // $logger = new FileAdapter(APP_PATH . '/app/runtime/apperror.log');
    //$logger->error('redis-connect-error: redis connect result ' . json_encode($res));
//}

$this->logger->error('redis-connect-error: redis connect result ' . json_encode($res));

return $cache;

});

when run php script throw error: PHP Fatal error: Using $this when not in object context



40.0k
Accepted
answer

Use:

$this->getShared('logger')->error('redis-connect-error: redis connect result ' . json_encode($res));

Or:

$this->getLogger()->error('redis-connect-error: redis connect result ' . json_encode($res));

$this is not object

Or:

$this->getLogger()->error('redis-connect-error: redis connect result ' . json_encode($res));

work in Phalcon 3.0.x

it works fine! thank you!

work in Phalcon 3.0.x