1
在此類中,實例化的情況下,我收到以下錯誤:
「$這」(T_VARIABLE)在代碼第12行 「default_timestamp '=> $ this - > _ time,
我很困惑,因爲當對象被實例化時,我假設$ _time可以使用,但它看起來不是。我也嘗試'default_timestamp' => time()
,但也拋出了一個錯誤。我誤解了對象實例嗎?
class DateTimeHandler {
public $_date;
public $_time = 'xxx';
public $_datetime;
public $_timezone;
public $opts = array(
'default_timezone' => 'America/New_York',
'default_timestamp' => $this->_time,
'formats' => array(
'date' => 'Y-m-d',
'time' => 'g:ia',
'full' => 'Y-m-d H:i:s'
)
);
public function __construct() {
echo '<pre>', print_r($this->opts, true);
}
}
$d = new DateTimeHandler();
這工作,是有一個原因,你不能直接做呢? – Naterade
參考:http://php.net/manual/en/language.oop5.properties.php **初始化必須是一個常量值 - 也就是說,它必須能夠在編譯時進行評估,並且不能依賴在運行時信息中進行評估。** –