Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Laravel – Using $this when not in object context

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?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

class ConfigurationProvider implements ConfigurationProvider
{
    protected $conf;

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

}

My Controller

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

>Solution :

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();
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading