PHP错误:致命错误:常量表达式包含无效操作 [英] PHP Error : Fatal error: Constant expression contains invalid operations

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

问题描述

我遇到错误:

致命错误:常量表达式中包含无效运算符 第214行的config.php

Fatal error: Constant expression contains invalid operations in config.php on line 214

该行是:

 protected static $dbname = 'mydb_'.$appdata['id'];

我在引号中是否犯了任何错误?还是其他地方?

Whether I did any mistakes in quotes? Or somewhere else?

我对错误消息的搜索仅显示了不同的原因(功能定义中的动态默认值).

My search for the error message only showed a different source cause (a dynamic default value in a function definition).

推荐答案

来自

From the official Php documentation :

与其他任何PHP静态变量一样,静态属性只能在PHP 5.6之前使用文字或常量进行初始化;不允许使用表达式.在PHP 5.6和更高版本中,相同的规则与const表达式适用:只要可以在编译时对其求值,某些有限的表达式是可能的.

Like any other PHP static variable, static properties may only be initialized using a literal or constant before PHP 5.6; expressions are not allowed. In PHP 5.6 and later, the same rules apply as const expressions: some limited expressions are possible, provided they can be evaluated at compile time.

因此,您不能使用另一个变量来初始化静态变量.用常量字符串替换$appdata['id']或删除static属性.

So you cannot initialize a static variable with another variable. Replace $appdata['id'] with a constant string or remove the static attribute.

这是因为,当其他变量的内容未知时, 所有静态声明都在编译时解析 (请参见

This is because all static declarations are resolved in compile-time, when the content of other variables is not known (see this other page of official doc).

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

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