PHP Error : Fatal error: Constant expression contains invalid operations

Php

Php Problem Overview


I am getting an error:

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

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

Php Solutions


Solution 1 - Php

From the official Php documentation :

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

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

Solution 2 - Php

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 string $dbname = DBNAME;

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

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

In your precise use case, however, it's possible that 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 string $dbname;

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

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

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

Solution 3 - Php

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

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.

Solution 4 - Php

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

Solution 5 - Php

For your information:- I got the same error by using some characters in a constant expressions.

public static $dbPassword="mAkE-34@-||sR";

This is what caused the error and I removed this || which is the logical OR operator characters from the string and it worked.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionRaja GopalView Question on Stackoverflow
Solution 1 - PhpAl Foиce ѫView Answer on Stackoverflow
Solution 2 - PhpÁlvaro GonzálezView Answer on Stackoverflow
Solution 3 - PhpMayank PandeyzView Answer on Stackoverflow
Solution 4 - PhpJackView Answer on Stackoverflow
Solution 5 - PhpSameeraView Answer on Stackoverflow