Difference in accessing arrays in PHP 5.3 and 5.4

2019-01-01 16:40发布

问题:

I\'m trying to access nested array element like this:

$dbSettings = $sm->get( \'Config\' )[ \'doctrine\' ][ \'connection\' ][ \'orm_default\' ][ \'params\' ];

It\'s inside Module.php of Zend\'s framework 2 project. $sm->get(\'Config\') return an array which I can access with code above locally, with PHP 5.4, while doing so on client\'s machine, it gives me an error:

Parse error: syntax error, unexpected \'[\' in /home/.../azk/module/Main/Module.php on line 121

Is there any difference in PHP 5.3 <=> 5.4 in accessing nested arrays or I have some default PHP configuration which is set differently on clients machne?

回答1:

Array dereferencing, which is what you are using, was introduced in PHP 5.4 and won\'t work in PHP 5.3.

So

$dbSettings = $sm->get( \'Config\' )[ \'doctrine\' ][ \'connection\' ][ \'orm_default\' ][ \'params\' ];

Would need to be:

$dbSettings = $sm->get( \'Config\' );
$params     = $dbSettings[ \'doctrine\' ][ \'connection\' ][ \'orm_default\' ][ \'params\' ];