数组/类常量表达式包含无效操作 [英] Array/classes Constant expression contains invalid operations

查看:101
本文介绍了数组/类常量表达式包含无效操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不明白为什么这行不通:

I can't understand why this doesn't work:

class TestOne
{

    public static $TEST = array(
        "test" => array( "name" => TestTwo::$TEST2[ "test" ] ) // error line
)}

class TestTwo
{
    public static $TEST2 = array(
        "test" => "result"
    );
}

这给了我错误:

常量表达式包含无效的操作

Constant expression contains invalid operations

我希望TestOne::$TEST[ "test" ][ "name" ]包含结果"

推荐答案

常量标量表达式无法引用变量(因为它们不是常量).

Constant scalars expressions cannot reference variables (as they are not constant).

您必须以其他方式(例如通过静态访问器)初始化属性,或者完全避免使用公共静态属性.

You'll have to initialize the property some other way (e.g. through a static accessor) or avoid public static properties altogether.

这篇关于数组/类常量表达式包含无效操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆