Benjamin W
Benjamin W

Reputation: 2848

Laravel pass config data into trait's property

trait Foo {
    private $url = config('api.url');

}

I have a url data set inside of config, however I need to put this value into trait's property. But it's not working. anyone know how to solve this problem?

what I did now is put construct inside of trait

public function __construct(){
        $this->url = config('api.url');
    }

Upvotes: 0

Views: 1037

Answers (1)

hassan
hassan

Reputation: 8288

it's not about traits, it's about php OOP nature itself:

here is the docs:

Class member variables are called "properties". You may also see them referred to using other terms such as "attributes" or "fields", but for the purposes of this reference we will use "properties". They are defined by using one of the keywords public, protected, or private, followed by a normal variable declaration. This declaration may include an initialization, but this initialization must be a constant value--that is, it must be able to be evaluated at compile time and must not depend on run-time information in order to be evaluated.

from the docs example:

// invalid property declarations:
public $var4 = self::myStaticMethod();
public $var5 = $myVar;

Upvotes: 1

Related Questions