1

Are their any good resources when you are trying to get a PHP script written in a newer version to work on an older version; Specifically 5.4 to 5.3?

I have even checked out articles about the changes and I cannot seem to figure out what I am doing wrong.


Here is the error I am getting, at this moment:

Parse error: syntax error, unexpected '[' in Schedule.php on line 113

And the code it is referring to:

private static $GAMES_QUERY = array('season' => null, 'gameType' => null);
.....
public function getSeason(){
$test = array_keys(self::$GAMES_QUERY)[0]; //<<<<<<<<<< line:113
return($this->query[$test]);
}

Everything I have seen seems to say that 5.3 had self::, array_keys, and the ability to access arrays like that.

hakre
  • 193,403
  • 52
  • 435
  • 836
Jonathon
  • 2,571
  • 5
  • 28
  • 49

3 Answers3

6

try...

$test = array_keys(self::$GAMES_QUERY);
$test = $test[0];

If I'm not mistaken, you can't use the key reference [0] in the same declaration in 5.3 like you can in 5.4 and javascript, etc.

zgr024
  • 1,175
  • 1
  • 12
  • 26
  • Ah, that does seem to fix it. Is there any syntax that allows that to be done in done in 1 line? – Jonathon Mar 20 '13 at 00:56
  • Thanks, I need to wait a few minutes before I am allowed to accept this. – Jonathon Mar 20 '13 at 01:01
  • like hakre answered you can use list() for a one liner but its another function call vs just a re-declaration... up to you to use either – zgr024 Mar 20 '13 at 13:48
5

That syntax was actually added in 5.4: http://docs.php.net/manual/en/migration54.new-features.php

So, you'll need a temporary variable to hold the result of the function, then access the index you want.

Chris Laplante
  • 29,338
  • 17
  • 103
  • 134
1

In versions lower than PHP 5.4 for the case you have you can make use of the list keywordDocs:

list($test) = array_keys(self::$GAMES_QUERY);

That works in PHP 5.4, too. But it does deal better with NULL cases than the new in PHP 5.4 array dereferencingDocs.

hakre
  • 193,403
  • 52
  • 435
  • 836