Jonathon
Jonathon

Reputation: 2701

PHP 5.4 to 5.3 Conversion

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.

Upvotes: 1

Views: 1319

Answers (3)

zgr024
zgr024

Reputation: 1173

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.

Upvotes: 6

hakre
hakre

Reputation: 197682

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.

Upvotes: 1

Chris Laplante
Chris Laplante

Reputation: 29658

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.

Upvotes: 5

Related Questions