Search code examples
phpslimslim-3

PHP Slim 3 Framework - Use MonoLog in Custom Class - Using $this when not in object context


Im am working from the Slim 3 skeleton and trying to use the MonoLog in a custom class I have created which is called Utilities.

Utilities.php - which is required from index.php

<?php

class Utilities {

    protected $logger;

    function __construct($c) {
        $this->logger = $logger;
    }

    static function checkPerms() {
        $this->logger->info("checkPerms() permissions of user id valid.");
        return true;
    }

}

Dependencies.php - I added the following:

$container['utilities'] = function ($c) {
    return new Utilities($c->get('logger'));   
};

But I am getting the error of:

Message: Using $this when not in object context

File: /Applications/MAMP/htdocs/project/src/utilities.php

I must be missing something but I am not sure what?


Solution

  • I would refactor Utilities.php a little bit:

    <?php
    
    class Utilities
    {
    
        protected $logger;
    
        function __construct($logger)
        {
            $this->logger = $logger;
        }
    
        public function checkPerms()
        {
            $this->logger->info("checkPerms() permissions of user id valid.");
    
            return true;
        }
    
    }