php 输出数组函数调用函数,php – 无法在数组中调用函数
標(biāo)簽:php
我試圖像這樣調(diào)用數(shù)組內(nèi)的函數(shù):
protected $settings = array(
'prefix' => $this->getPrefix(),
);
expression is not allowed as field of default value
getPrefix()
public function getPrefix()
{
return "hello world";
}
我不能這樣做?
解決方法:
根據(jù)受保護的關(guān)鍵字判斷,您正在嘗試設(shè)置對象屬性.根據(jù)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.
為了設(shè)置你的值,把它放到構(gòu)造函數(shù)中:
class Settings
{
protected $settings;
public function __constructor() {
$this->settings = array(
'prefix' => $this->getPrefix(),
);
}
public function getPrefix() {
return "Hello, World!";
}
}
標(biāo)簽:php
來源: https://codeday.me/bug/20190528/1167788.html
總結(jié)
以上是生活随笔為你收集整理的php 输出数组函数调用函数,php – 无法在数组中调用函数的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: html div element,你能在
- 下一篇: js mysql json字符串转数组中