If I use a "dot" to assign a value in a variable in a PHP class, it fails.
For example:
class bla {
public $a = 'a' . 'b';
}
How should I approach this otherwise?
You can only do that in the constructor, as class variables/properties must be initialized on declaration with constant expressions. From the manual:
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.
This means you can't use any operators or function calls.
class bla {
public $a;
public function __construct() {
$this->a = 'a' . 'b';
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With