常量的字符串连接 [英] string concatenation with constants
本文介绍了常量的字符串连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
当前的托管服务商仅提供PHP 5.4,并且在代码中始终会出现类常量错误.显然,不允许定义数组常量.我将常量更改为静态变量以使其正常工作.现在,我收到此语法错误:
I have only PHP 5.4 available at my current hoster and I always get errors with class constants in my code. Apparently, it's not allowed to define array constants. I changed the constant to a static variable to make it work. Now I get this syntax error:
syntax error, unexpected '.', expecting ']'
我尝试定义由串联常量组成的字符串.
I try to define strings that consist of concatenated constants.
public static $arr = [KEY_ONE => "string " . MyClass::CONSTANT . " string"]
这是否可能,或者现在所有常量都必须是静态变量吗?
is this possible or do all constants have to be static variables now?
推荐答案
在变量声明中,您无法执行操作.既不是串联运算,也不是数学运算.
In the variable declaration you cannot do operations. Neither concatenation nor math operations.
您可以通过构造方法来完成;
You can do it in construct method;
public static $arr = [];
public function __construct(){
self::$arr = [KEY_ONE => "string " . MyClass::CONSTANT . " string"];
}
这篇关于常量的字符串连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文