简体   繁体   中英

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

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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM