2

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'
  );
}
idoimaging
  • 764
  • 1
  • 7
  • 14
  • possible duplicate of [Workaround for basic syntax not being parsed](http://stackoverflow.com/questions/2671928/workaround-for-basic-syntax-not-being-parsed) – mario Dec 21 '11 at 02:01
  • 1
    you should accept the answer below to reward the person who helped you. :) – Tim G Dec 21 '11 at 02:27

1 Answers1

6

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'
  );
}
  • Thanks. I had thought it would be constant. Though I do now see the point that the operator makes it an expression. – idoimaging Dec 21 '11 at 02:06