PHP 错误:致命错误:常量表达式包含无效操作

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/40171546/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 02:08:08  来源:igfitidea点击:

PHP Error : Fatal error: Constant expression contains invalid operations

php

提问by Raja Gopal

I am getting an error:

我收到一个错误:

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

致命错误:常量表达式在第 214 行的 config.php 中包含无效操作

That line was:

那一行是:

 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).

我对错误消息的搜索只显示了不同的源原因(函数定义中的动态默认值)。

采纳答案by Al Foиce ?

From the official Php documentation:

来自官方 PHP 文档

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.

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

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

所以你不能用另一个变量初始化一个静态变量。替换$appdata['id']为常量字符串或删除该static属性。

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).

这是因为所有静态声明都在编译时解析,当其他变量的内容未知时(请参阅官方文档的其他页面)。

回答by álvaro González

Unless you mess with reflection, the only way I can think of to have a static private/protected class property with a dynamically generated value is to calculate it outside the class:

除非你弄乱了反射,否则我能想到的唯一方法是拥有一个动态生成的值的静态私有/受保护的类属性是在类之外计算它:

class Foo {
    protected static $dbname = DBNAME;

    public static function debug() {
        return Foo::$dbname;
    }
}

$appdata = array(
    'id' => 31416,
);
define('DBNAME', 'mydb_'.$appdata['id']);
var_dump(Foo::debug());

In your precise use case, however, it's possiblethat there's simply no good reason for the property to be static. In that case, it's as straightforward as using the constructor:

但是,在您的精确用例中,该属性可能根本没有充分的理由是静态的。在这种情况下,就像使用构造函数一样简单:

class Foo {
    protected $dbname;

    public function __construct($appdata){
        $this->dbname = 'mydb_'.$appdata['id'];
    }

    public function debug() {
        return $this->dbname;
    }
}

$appdata = array(
    'id' => 31416,
);
$foo = new Foo($appdata);
var_dump($foo->debug());

回答by Mayank Pandeyz

Raja this is because a static variable contains a constant value in it. But in your case:

Raja 这是因为静态变量中包含一个常量值。但在你的情况下:

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

$appdata['id']is dynamic that can change its value during the execution. That's why the error is showing.

$appdata['id']是动态的,可以在执行期间更改其值。这就是错误显示的原因。

回答by Hyman

I had this error and my fix was to not declare a date within a class property array

我遇到了这个错误,我的解决方法是不在类属性数组中声明日期

public static $config_array = array(
    'start_date' => date('Y-m-d H:i:s') // No can do
);