douyun1852
2017-03-13 12:38
浏览 111
已采纳

Laravel将配置数据传递给trait的属性

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

图片转代码服务由CSDN问答提供 功能建议

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

我在config中有一个url数据集,但我需要将此值放入trait的属性中。 但它不起作用。 有谁知道如何解决这个问题?

我现在所做的是将构造置于特征中

  public function __construct(){\  n $ this-> url = config('api.url'); 
} 
   
 
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dongzhi1949 2017-03-13 12:46
    已采纳

    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;
    
    点赞 打赏 评论

相关推荐 更多相似问题