NYCPHP Meetup

NYPHP.org

[nycphp-talk] Arrays and string concat in classes

Dan Cech dcech at phpwerx.net
Mon Apr 24 12:52:40 EDT 2006


Michael Haszprunar wrote:
> <lurk mode off>
> Hi folks,
> 
> Today I found an interesting/anoying thing regarding classes in PHP4.
> What I tried was this:
> 
> class blabla
> {
> 	var $bla = array(
> 				'test' => MY_CONSTANT.'some text'
> 			);
> }
> 
> and PHP threw an error ("syntax error, unexpected '.', expecting ')'"). 
> It seems that string concatenation is not allowed.
> 
> Is this a bug or a feature? Why should this not be allowed?
> 
> Thanks and also many thanks for all the other interessting things I can 
> read on that mailing list.
> 
> Kind regards from Munich, Germany
> 
> Michael
> 
> </lurk mode on>

Michael,

This is because you cannot have evaluated code in the class definition.

If you were to use the construct:

class blabla
{
  var $bla;

  function blabla()
  {
    $this->bla = array(
      'test' => MY_CONSTANT.'some text'
    );
  }
}

That would work because the concatenation is evaluated when the object
is instantiated.

Dan



More information about the talk mailing list