When I try to do the following I get a syntax error, unexpected T_VARIABLE. What am I doing wrong?
class myObj {
public $birth_month;
public $birthday = array('input_val' => $this->birth_month);
}
I also tried
class myObj {
public $birth_month;
public $birthday = array('input_val' => $birth_month);
}
You cannot use an expression to initialize a class property. It must be a constant value, or you must initialize it in the constructor. That’s the source of your syntax error.
From the docs on class properties:
In your first attempt, using
$thisoutside an instance method would not have been supported even baring the compile-time limitation of property initialization, since$thisis only meaningful inside instance methods.