2

In my project i used following way to store and fetch data from session.

To write data:

$this->Session->write('data', array('title'=>'Abc'));

To read data:

$this->Session->read('data')['title'];

And it is still working fine at live server. 2 days ago i shifted my code to some other live server. Now this server is showing fatal error at $this->Session->read('data')['title']; i.e unexpected [ ]

Then i google and found that there a some other way to get index e.g

$this->Session->read('data.title');

if $this->Session->read('data')['title'] is the wrong way then how it is still working at my old server.

Thanks in advance.

Shaminder Singh
  • 1,283
  • 2
  • 18
  • 31

1 Answers1

0

[] is supported in PHP 5.4 This is a short syntax and in PHP < 5.4 it won't work.

Source: PHP Difference between array() and []




Here's what you are looking for. I posted above content in a hurry.

As of PHP 5.4 it is possible to array dereference the result of a function or method call directly. Before it was only possible using a temporary variable.

Source

Example:

function getArray() {
    return array(1, 2, 3);
}

// on PHP 5.4
$secondElement = getArray()[1];

// previously
$tmp = getArray();
$secondElement = $tmp[1];
Community
  • 1
  • 1
user3082321
  • 654
  • 5
  • 12