mercredi 26 mai 2021

Assigning a variable's value to a constant in PHP [duplicate]

I have a config file that contains a constant array that is to be used globally throughout my script.

const ENV = [
        'project_name' => '*',
        'database' => [
            'db' => '*',
            'username' => '*',
            'password' => '*',
            'host' => 'localhost',
            'port' => '3306',
        ],
        'host' => $_SERVER['HTTP_HOST'],
        'ssl' => ($_SERVER["REQUEST_SCHEME"] == 'https') ? true : false,
    ];

When executing the above, I get the following error:

Fatal error: Constant expression contains invalid operations in */config.php on line 3

The reason why I'm trying to have everything predefined in a constant is really not relevant at this stage as this decision was kind of out of my hands.

Please note, I have other values as well that are to be calculated and dealt with before they can be assigned to the constant. Therefore, would love to get to know of a way to store a value (not address to the variable) in the constant array.




Aucun commentaire:

Enregistrer un commentaire