标签:php
我试图像这样调用数组内的函数:
protected $settings = array(
'prefix' => $this->getPrefix(),
);
expression is not allowed as field of default value
getPrefix()
public function getPrefix()
{
return "hello world";
}
我不能这样做?
解决方法:
根据受保护的关键字判断,您正在尝试设置对象属性.根据PHP manual:
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.
为了设置你的值,把它放到构造函数中:
class Settings
{
protected $settings;
public function __constructor() {
$this->settings = array(
'prefix' => $this->getPrefix(),
);
}
public function getPrefix() {
return "Hello, World!";
}
}
标签:php
来源: https://codeday.me/bug/20190528/1167788.html