tags:

views:

80

answers:

3

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

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

instead you have to do this

$user = Auth::getFullUser();
echo $user[ 'country' ];
+5  A: 

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.

Coronatus
great thanks for clearing that up, just making sure that there wasnt a better way.
David Morrow
A: 

Poor language/interpreter design.

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

Kendall Hopkins
Not poor in the slightest. If you don't like the language, there are tons of other ones for you to choose from.
Coronatus
yeah its not poor, I just like to save writing an extra variable... was just curious.
David Morrow
Inconvenient, lets say, and you have to understand that Kendall Hopkins uses PHP too. We are not haters, just honest PHP users.
erisco
@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
+5  A: 

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())

Anthony Forloney