Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Initializing PHP class property declarations with simple expressions yields syntax error

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

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

I'm trying to initialize an array and having some issues. While this works fine:

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

This creates a syntax error:

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

Even this isn't accepted:

public $var = 4+1;

which suggests it's not a limitation of the array() language construct.

Now, the last time I checked, "4+1" equated to a constant value that not only should be accepted, but should in fact be optimized away. In any case, it's certainly able to be evaluated at compile-time.

So what's going on here? Is the limitation really along the lines of "cannot be any calculated expression at all", versus any expression "able to be evaluated at compile time"? The use of "evaluated" in the doc's language suggests that simple calculations are permitted, but alas....

If this is a bug in PHP, does anyone have a bug ID? I tried to find one but didn't have any luck.

like image 235
user171929 Avatar asked Apr 24 '10 01:04

user171929


People also ask

What will be the syntax of defining the class in PHP?

Basic class definitions begin with the keyword class , followed by a class name, followed by a pair of curly braces which enclose the definitions of the properties and methods belonging to the class. The class name can be any valid label, provided it is not a PHP reserved word.

How to get property of object in PHP?

The get_object_vars() function is an inbuilt function in PHP that is used to get the properties of the given object.


2 Answers

PHP doesn't do such operations at compile-time; you cannot assign calculated values to constants, even if all operators are constants themselves. Default values of class members are treated the exact same way. I encountered this behaviour as I tried to assign powers of two to constants:

class User {
    const IS_ADMIN = 1;
    const IS_MODERATOR1 = 1 << 1; // Won't work
    const IS_MODERATOR2 = 0x02;   // works
}
like image 148
soulmerge Avatar answered Nov 11 '22 01:11

soulmerge


This limitation no longer exists as of PHP 5.6

The new feature that enables the previously-disallowed syntax is called constant scalar expressions:

It is now possible to provide a scalar expression involving numeric and string literals and/or constants in contexts where PHP previously expected a 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 will output:

4 The value of THREE is 3
like image 36
Jon Avatar answered Nov 11 '22 03:11

Jon