2016-12-14 64 views
3

這裏是我的代碼:爲什麼初始化屬性不起作用?

class Log 
{ 
    private $mode = config('my.log.mode'); 
} 

但它拋出語法錯誤 ..!爲什麼?怎麼了?

注意到這個作品,以及:

class Log 
{ 
    private $mode; 

    public function __construct() 
    { 
     $this->mode = config('my.log.mode'); 
    } 
} 

好,有什麼意義?

回答

4

只能用常量初始化屬性。所以,你不能在這裏使用config()或任何其他幫手。

初始化必須是一個恆定值 - 也就是說,它必須能夠在編譯時進行評估,並以不能依賴於運行時的信息進行評估

http://php.net/manual/en/language.oop5.properties.php

+0

..或PHP 5.6的標量表達式:http://docs.php.net/manual/en/migration56.new-features.php#migration56.new-features.const-scalar-exprs – simon