Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

silly php question... why cant i do this?

echo Auth::getFullUser()[ 'country' ];

instead you have to do this

$user = Auth::getFullUser();
echo $user[ 'country' ];
share|improve this question
1  
I suppose you'd be able to do this: echo ( Auth::getFullUser() )[ 'country' ]; –  Jacob Relkin May 20 '10 at 15:24
2  
no, you're not. –  VolkerK May 20 '10 at 15:30
add comment

3 Answers

up vote 5 down vote accepted

The syntax just doesn't allow it unfortunately.

AFAIK there was at one time intention to put that syntax in PHP6, but it has been dropped.

share|improve this answer
 
great thanks for clearing that up, just making sure that there wasnt a better way. –  David Morrow May 20 '10 at 15:38
add comment

PHP grammar only allows subscript notation (i.e. ['country']) on the end of a variable expression (i.e. $user) not an expression (i.e. Auth::getFullUser())

share|improve this answer
add comment

Poor language/interpreter design.

Same reason you can't do "functionname"() and functions are case insensitive.

share|improve this answer
1  
Not poor in the slightest. If you don't like the language, there are tons of other ones for you to choose from. –  Amy B May 20 '10 at 15:33
 
yeah its not poor, I just like to save writing an extra variable... was just curious. –  David Morrow May 20 '10 at 15:38
1  
Inconvenient, lets say, and you have to understand that Kendall Hopkins uses PHP too. We are not haters, just honest PHP users. –  erisco May 20 '10 at 19:45
1  
@Coronatus @David Morrow I stand behind my statement that PHP, both language and implementation, is bad. There is NO formal definition of the language. How the language is "defined" is largely derived from the ZEND implementation and the informal and ambiguous documentation on php.net. There is little to no effort to make the syntax more generalized such as languages like C, C++, Haskell, Python to handle edge cases like this. Saying PHP is "not poor in the slightest", is complete garbage when you consider even the many inconstancies in the naming and param order of standard functions. –  Kendall Hopkins May 21 '10 at 3:19
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.