php 输出数组函数调用函数,php – 无法在数组中调用函数

标签: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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值