This code gives the error "unexpected '.', expecting ')'". Why is this invalid? I'd thought that as both parts are constant, I could concatenate them. New to PHP. Thanks.
class c {
const HELLO = 'hello';
public $arr = array(
'hw' => self::HELLO . 'world'
);
}
Answer
Class properties must have constant initial values. The concatenation of those two strings is NOT a constant value.
From the documentation:
[Property] 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.
You could put the property initialisation in your constructor:
public function __construct()
{
$this->arr = array(
'hw' => self::HELLO . 'world'
);
}
No comments:
Post a Comment