skip to Main Content

I am working on a PHP library in Laravel. I want to call a variable that is global to my function and I get this error.

Using $this when not in object context

Where am I missing?

class ConfigurationProvider implements ConfigurationProvider
{
    protected $conf;

    private function getCredentials()
    {
        return $this->conf;
    }

}

My Controller

public function testApi(){
        $object = ConfigurationProvider::getCredentials();
        return $object;
}

2

Answers


  1. Instantiate your object, then call the method

    public function testApi() 
    {
       $configProvider = new ConfigurationProvider();
       $object = $configProvider->getCredentials();
       return $object;
    }
    
    Login or Signup to reply.
  2. You’re treating getCredentials() as a static method. Static method do not have access to the object. Just static properties.

    class ConfigurationProvider implements ConfigurationProvider
    {
        protected static $conf;
    
        private static function getCredentials()
        {
            return self::$conf;
        }
    }
    
    $object = ConfigurationProvider::getCredentials();
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search