-1

I am receiving the error:

Message : syntax error, unexpected '*', expecting ',' or ';'

when I try to use math symbols to define a variable inside a class. For example,

class Foo {
    public static $test = 1+1;
}

echo Foo::$test;

yields this error. Is it possible to use math within class variable definitions in PHP?

1 Answers1

1

Expression isn't allowed as field default value. You may try this

class Foo {
    public static $test;

    public function __construct(){
        $this->test = 1+1;
    }
}

echo Foo::$test;
sas
  • 2,563
  • 1
  • 20
  • 28