Why is some_function()[some_index] invalid syntax?

This is a discussion on Why is some_function()[some_index] invalid syntax? within the PHP General forums, part of the PHP Programming Forums category; On Friday 11 January 2008, Zoltán Németh wrote: > > So, make all your functions return objects, and ...


Go Back   Usenet Forums > PHP Programming Forums > PHP General

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #11 (permalink)  
Old 01-12-2008
Larry Garfield
 
Posts: n/a
Default Re: [PHP] Why is some_function()[some_index] invalid syntax?

On Friday 11 January 2008, Zoltán Németh wrote:

> > So, make all your functions return objects, and have the object have a
> > method called get or index or something like that that returns the index
> > requested. :)
> >
> > Better yet, make everything an object: String, Numeric, Array, etc

>
> and call it Java ;)


More like C#. :-) Java still has primitives.

--
Larry Garfield AIM: LOLG42
larry@garfieldtech.com ICQ: 6817012

"If nature has made any one thing less susceptible than all others of
exclusive property, it is the action of the thinking power called an idea,
which an individual may exclusively possess as long as he keeps it to
himself; but the moment it is divulged, it forces itself into the possession
of every one, and the receiver cannot dispossess himself of it." -- Thomas
Jefferson
Reply With Quote
  #12 (permalink)  
Old 01-13-2008
Daniel
 
Posts: n/a
Default Re: [PHP] Why is some_function()[some_index] invalid syntax?

Nathan Nobbe wrote:
> On Jan 11, 2008 3:45 AM, Zoltán Németh <znemeth@alterationx.hu> wrote:
>
>> and call it Java ;)
>>

>
> or perhaps javascript :)
> function cool() {
> return [1, 2, 3];
> }
> alert(cool()[0]);
>
> -nathan
>

I too thought this was weird...

example:
<?php
function a($key)
{
$data = array(1 => array(1 => 'hello'), 2 => array(1 => 'good day'));
return $data[$key];
}
echo a(1)[1];
?>

Of course this is a completely useless example, but being able to do
syntax like that would be nifty, as you could do something like
$User->FindUserByID(73)['username'] if the method returned an array of
user data.
Reply With Quote
  #13 (permalink)  
Old 01-14-2008
Richard Lynch
 
Posts: n/a
Default Re: [PHP] Why is some_function()[some_index] invalid syntax?

On Thu, January 10, 2008 10:00 pm, Arlen Christian Mart Cuss wrote:
> Why is it that if I try to evaluate an index of an array returned by a
> function immediately, a syntax error is produced? (unexpected '[',
> expecting ',' or ';')


Because PHP is not C.

It's language-design was chosen to not let you write complicated
expressions that only confuse beginners.

There was talk of letting this work on php-internals, but it was shot
down, as I recall...

Or perhaps not, but nobody actually submitted a patch to make it work,
so it's one of those "someday" features...

--
Some people have a "gift" link here.
Know what I want?
I want you to buy a CD from some indie artist.
http://cdbaby.com/from/lynch
Yeah, I get a buck. So?
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT +1. The time now is 04:13 AM.


Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0