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.
syntax properties php class
user171929
source share