Solved thread

This post is marked as solved. If you think the information contained on this thread must be part of the official documentation, please contribute submitting a pull request to its repository.

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



15.9k
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:

```php $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