Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
191 views
in Technique[技术] by (71.8m points)

Difference in accessing arrays in PHP 5.3 and 5.4 or some configuration mismatch?

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?

Question&Answers:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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' ];

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...