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 ...
|
|||||||
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
|
|||
|
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 |
|
|||
|
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. |
|
|||
|
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? |
![]() |
| Thread Tools | |
| Display Modes | |
|
|