0

I have Apache/PHP/MySQL installed locally on my computer to quickly test stuff out. One of the things I'm currently working on is a hexagon chart for a statistics in a game created dynamically via the GD library.

On my laptop everything worked OK as I'm getting a nice result. image1

However when I deploy it onto the server I bought I'm getting a syntax error:

Parse error: syntax error, unexpected '['

Coming from this statement:

$hexstat = new CL2HexStatPoints( 
    CL2StatsRepository::GetClassesFromJson()[$id], <- *error here*
    new CHexagon( CVector2::Create( constant("RADIUS"), constant("RADIUS") ) 
    ) 
);

It doesn't seem to like the index operator on there. The function itself parses a json file and returns an array of objects containing properties about player classes. I've confirmed that it is indeed an array by using var_dump. However if I program it like this:

$array = CL2StatsRepository::GetClassesFromJson();
var_dump( $array[$id] ); // this is ok
var_dump( CL2StatsRepository::GetClassesFromJson()[$id] ); // this isn't

The first statement is OK, the second one isn't. The version of PHP I have on my work computer is 5.4.12 and the version of PHP on the server is 5.3.22. I'm assuming something has changed involving the operator between version or there is maybe a setting I need to change. I haven't really been able to find any information either searching around, is there anybody that could perhaps shed some light on why this is? Thanks a lot.

EDIT: I've fixed the code to be compatible with the old PHP version so I'll keep that in mind from now on. If anybody is interested in seeing it work for real: http://www.qzmpox.nl/l2/hexagon/hexagontest.php

  • 1
    Your assumption is correct. Dereferencing from function calls was only introduced in PHP 5.4. On a side note, this is why you want to keep your development environment as similar as possible to your production environment. – Gordon Mar 22 '13 at 16:21
  • 1
    This syntax has been added in PHP 5.4. See [here](http://php.net/manual/en/language.types.array.php#example-88) – juco Mar 22 '13 at 16:22
  • Thanks Gorden, Juco, and also Rafal below. I had actually just purchased the hosting so I assumed that it already had the latest version. I had only just checked the PHP version now. Thanks for the help guys. – Bryan Anthony Abrams Mar 22 '13 at 16:26
  • Thanks michi, I've added the link to the working script in the post! – Bryan Anthony Abrams Mar 22 '13 at 16:47

1 Answers1

1

You cannot instantly access "index" of result in PHP < 5.4, which is why second piece of code doesn't work on your server

stryjek4
  • 26
  • 2