PHP Error: Fatal Error: Constant Expression Contains Invalid Operations

PHP Error : Fatal error: Constant expression contains invalid operations

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

PHP 7, Fatal error: Constant expression contains invalid operations

As you can see by the line of code you posted you used a single double quote in the function declaration. If you look closely you'll see that ,pwd=" is all in red because of the single double quote after $email=

Either change the double quote to 2 single quotes or add another double quote.

function validate($dbc, $email='',$pwd='')

OR

function validate($dbc, $email="",$pwd="")

Constant expression contains invalid operations

As described here

Class member variables are called "properties". You may also see them referred to using other terms such as "attributes" or "fields", but for the purposes of this reference we will use "properties". They are defined by using one of the keywords public, protected, or private, followed by a normal variable declaration. This declaration may include an initialization, but this initialization must be a constant value--that is, it must be able to be evaluated at compile time and must not depend on run-time information in order to be evaluated.

The only way you can make this work is :-

<?php

namespace App;

class Amazon
{
protected $serviceURL;

public function __construct()
{
$this->serviceURL = config('api.amazon.service_url');
}
}

Fatal error: Constant expression contains invalid operations in /PATH/initClass.php on line 5

Your problem is that you are using a function operation to set a value to a class variable. To fix your problem, use the following code (i.e. move initialization to the constructor)

<?php
Class init
{
public const THEME = "aman/dev/frontend/";
private $root;

public function __construct() {
$this->root = dirname(__dir__)."/aman/dev/fontend/";
}

public function getFile($name,$value)
{
list(
$title
) = $value;


}
}
?>

PHP Fatal error: Constant expression contains invalid operations, when initializing a static inline?

Based on php documentation; You cannot initialize static variable with another non constant expression or variable.

Which means if you want to assign a value to static variable this value should be a an integer, string etc.

What you did here is against static word rule in PHP you are assigning a dynamic value to $timezones variable

static $timezones = DateTimeZone::listIdentifiers(); // Error here

Check for detailed information.

http://php.net/manual/en/language.variables.scope.php#language.variables.scope.static



Related Topics



Leave a reply



Submit