Initializing PHP class property declarations with simple expressions gives a syntax error

According to the PHP docs, you can initialize properties in classes with the following restriction:

"This declaration may include initialization, but this initialization must be a constant value, that is, it must be able to be evaluated at compile time and not be dependent on runtime information for evaluation."

I am trying to initialize an array and have some problems. Although this works great:

public $var = array( 1 => 4, 2 => 5, ); 

This creates a syntax error:

 public $var = array( 1 => 4, 2 => (4+1), ); 

Even this is not accepted:

 public $var = 4+1; 

which indicates that this is not a limitation of the construction of the array () language.

Now, the last time I checked, "4 + 1" is equated to a constant value that not only needs to be accepted, but should actually be optimized. In any case, this can certainly be evaluated at compile time.

So what is going on here? Is the restriction really on the lines “there can be no computed expression at all”, compared to any expression “can be evaluated at compile time”? The use of "graded" in the document language suggests that simple calculations are allowed, but alas ....

If this is an error in PHP, does anyone have an error identifier? I tried to find him, but he was out of luck.

+6
syntax properties php class
source share
3 answers

PHP does not perform such operations at compile time; you cannot assign computed values ​​to constants, even if all the operators are constants themselves. The default values ​​for class members are handled in exactly the same way. I came across this behavior as I tried to assign degrees from two constants:

 class User { const IS_ADMIN = 1; const IS_MODERATOR1 = 1 << 1; // Won't work const IS_MODERATOR2 = 0x02; // works } 
+9
source share

This restriction no longer exists with PHP 5.6

A new function that allows previously forbidden syntax is called constant scalar expressions :

You can now provide a scalar expression that includes numeric and string literals and / or constants in contexts where PHP previously expected static value, such as constant and property declarations and default function arguments.

 class C { const THREE = TWO + 1; const ONE_THIRD = ONE / self::THREE; const SENTENCE = 'The value of THREE is '.self::THREE; public function f($a = ONE + self::THREE) { return $a; } } echo (new C)->f()."\n"; echo C::SENTENCE; ?> 

The above example outputs:

 4 The value of THREE is 3 
+7
source share

Before you raise your hands up in php, think about the execution model. In an environment in which php is commonly used (and, in fact, intended for), everything is created, executed, and then thrown ... until the next incoming HTTP request. It doesn't make much sense to spend time doing the calculations during the parsing / compilation phase. In general, the engine should be very fast.

But, you are right, this quote from the manual really says "rate it." Perhaps you should open a ticket for documentation.


Change March 2014

it looks like php will now support Constal Scalar Expressions in php 5.6:

+6
source share

All Articles