下面提到的代码有什么问题?它正在引发解析错误PHP分析错误:语法错误,意外的".",应为","或";"在第9行上的/home/gaurav/c.php中
<?php
class b {
const ABC = 'abc';
}
class c extends b {
private $r = self::ABC ." d";
function getABC()
{
echo $this->r;
}
}
$c = new c();
$c->getABC();
因此private $r = self::ABC ." d";
是不允许的,而private $r = self::ABC;
是可以的,正如Gautam3164所回答的。
更多详细信息,例如在这个答案中:使用简单表达式初始化PHP类属性声明会产生语法错误
从http://www.php.net/manual/en/language.oop5.properties.php
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.
所以,在我自己的代码中::ABC。"d"不是常数。
您可以尝试:
private $r = self::ABC;
$r = $r . "d";