2

on my localhost [PHP Version 5.5.9-1ubuntu4.5] this code is working:

array($userName => ['score' => $score]);

and also this code is working:

$this->Auth->user()['id']

but on production server [PHP Version 5.3.3-7+squeeze23] in both cases I've got an error:

Error: Fatal Error (4): syntax error, unexpected '['

what's going on? how can I fix it in a simplest way? (cause changing all arrays in project is highly impracticable and I'm even not sure how to manage the second case with Auth...)

Michal_Szulc
  • 4,097
  • 6
  • 32
  • 59

3 Answers3

4

The first is because the new [] syntax for instantiating arrays only works in 5.4 and above. So, replace it with array():

// 5.4+ only:
array($userName => ['score' => $score]);
// 5.3 (and earlier) and 5.4+
array($userName => array('score' => $score));

The second is a different 5.4 feature, that of accessing arrays returned from functions, where you should use a temporary variable:

// 5.4+ only:
$this->Auth->user()['id']
// 5.3 (and earlier) and 5.4+:
$result = $this->Auth->user()
$result[id]

Or, for preference, upgrade your production server to a PHP version that's a bit more modern than the four-or-five year old version you're using. To save on more of these headaches, you either need to do that or start developing locally in 5.3. (If you need to do the latter, I'd look into virtualising your development setup, so you could develop in a virtual box against 5.3 for older production systems.)

Community
  • 1
  • 1
Matt Gibson
  • 37,886
  • 9
  • 99
  • 128
3

That syntax isn't supported until php version 5.4

You can see that here:

http://php.net/manual/en/language.types.array.php

Get Off My Lawn
  • 34,175
  • 38
  • 176
  • 338
  • ok, now it's clear. thanks a lot! ...in fact I was afraid of this answer. the night with fixing arrays is in front of me ;/ – Michal_Szulc Jan 04 '15 at 21:39
1

The squarebracket Syntax for Arrays was introduced in php v. 5.4. Same goes for the usage of returned values of a function or method

Jojo
  • 2,720
  • 1
  • 17
  • 24