Home > Blockchain >  Laravel - Using $this when not in object context
Laravel - Using $this when not in object context

Time:09-26

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;
}

CodePudding user response:

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();

CodePudding user response:

Instantiate your object, then call the method

public function testApi() 
{
   $configProvider = new ConfigurationProvider();
   $object = $configProvider->getCredentials();
   return $object;
}
  • Related