Feature request

php.internals

Dmitry Shirokov

19 years ago
Hey guys. What are you thinking about adding this feature: <?php function foo() { return array(1,2,3,4,5,6); } echo foo()[4]; // <---- it that // or may be (foo())[4] ? // instead of $var = foo(); echo $var[4]; ?>
-- Thanks, Dmitry

Edin Kadribasic

19 years ago
Dmitry Shirokov wrote:
> Hey guys. > > What are you thinking about adding this feature: > > <?php > function foo() > { > return array(1,2,3,4,5,6); > } > > echo foo()[4]; // <---- it that > // or may be (foo())[4] ? > > > // instead of > $var = foo(); > echo $var[4];
+1 I would very much like this feature in PHP. Edin

Evert Pot

19 years ago
Edin Kadribasic wrote:
> Dmitry Shirokov wrote: > >> Hey guys. >> >> What are you thinking about adding this feature: >> >> <?php >> function foo() >> { >> return array(1,2,3,4,5,6); >> } >> >> echo foo()[4]; // <---- it that >> // or may be (foo())[4] ? >> >> >> // instead of >> $var = foo(); >> echo $var[4]; >> > > > +1 I would very much like this feature in PHP. > > Edin > >
Me too, I know I don't have any voting rights here, but I'm running into this one every day. Evert

Stanislav Malyshev

19 years ago
>>> <?php >>> function foo() >>> { >>> return array(1,2,3,4,5,6); >>> } >>> >>> echo foo()[4]; // <---- it that
It looks a bit perl-ish indeed but I don't see much trouble in having that it people really need it...
-- Stanislav Malyshev, Zend Products Engineer stas@zend.com http://www.zend.com/

Andi Gutmans

19 years ago
There are some cases where this could start becoming quite challenging and we might get into reference counting/separation problems, i.e.: function foo() { return array(1,2,3); } foo()[1] = 4; or function foo() { $arr = array(); $arr[1] =& $var; } foo()[1] = 2; Anyway, these are the kind of things what we'd need to consider and with a few more minutes I can start thinking of some rougher ones where we'd need to return by reference and gracefully handle when you don't return by reference... I'm not opposed to this kind of syntax but it needs quite a lot of thorough thinking and it's a great example of where test-driven development would really be suitable (have like 20 of these weird cases in tests and try and understand if we can get this 100% right). Andi

Stanislav Malyshev

19 years ago
> There are some cases where this could start becoming quite challenging and > we might get into reference counting/separation problems, i.e.: > > function foo() > { > return array(1,2,3); > > } > foo()[1] = 4;
I'd say expression(f()[1]) means $tmp = f(); expression($tmp); unset($tmp); which here means: $tmp = foo(); $tmp[1] = 4; unset($tmp); which is meaningless but should work. IIRC the engine can make free's at the end of expression, so it shouldn't be big problem. Actually, any assignment to it if it's not returned by-ref is meaningless, but syntactically ok.
-- Stanislav Malyshev, Zend Products Engineer stas@zend.com http://www.zend.com/

Rick Widmer

19 years ago
Stanislav Malyshev wrote:
> I'd say expression(f()[1]) means $tmp = f(); expression($tmp); > unset($tmp); which here means: > $tmp = foo(); > $tmp[1] = 4; > unset($tmp); > > which is meaningless but should work. IIRC the engine can make free's at > the end of expression, so it shouldn't be big problem. Actually, any > assignment to it if it's not returned by-ref is meaningless, but > syntactically ok.
foo() = 4; results in: Fatal error: Can't use function return value in write context in test on line 12. foo()[1] = 4; should do the same.

Alain Williams

19 years ago
On Fri, Nov 10, 2006 at 05:05:05AM -0700, Rick Widmer wrote:
> > > Stanislav Malyshev wrote: > >I'd say expression(f()[1]) means $tmp = f(); expression($tmp); > >unset($tmp); which here means: > >$tmp = foo(); > >$tmp[1] = 4; > >unset($tmp); > > > >which is meaningless but should work. IIRC the engine can make free's at > >the end of expression, so it shouldn't be big problem. Actually, any > >assignment to it if it's not returned by-ref is meaningless, but > >syntactically ok. > > > foo() = 4; results in: > > Fatal error: Can't use function return value in write context in test on > line 12. > > foo()[1] = 4; should do the same.
No, what if they return a reference to something ? In the first case a simple variable, in the second case an array. It those cases assigning to what foo() returns would be reasonable. If foo were to return a constant or a value (not a reference) then the above should be errors. Since functions don't have fixed return types the error test needs to be done at run time.
-- Alain Williams Parliament Hill Computers Ltd. Linux Consultant - Mail systems, Web sites, Networking, Programmer, IT Lecturer. +44 (0) 787 668 0256 http://www.phcomp.co.uk/ #include <std_disclaimer.h>

Richard Lynch

19 years ago
On Thu, November 9, 2006 11:48 pm, Stanislav Malyshev wrote:
>> There are some cases where this could start becoming quite >> challenging and >> we might get into reference counting/separation problems, i.e.: >> >> function foo() >> { >> return array(1,2,3); >> >> } >> foo()[1] = 4; > > I'd say expression(f()[1]) means $tmp = f(); expression($tmp); > unset($tmp); which here means: > $tmp = foo(); > $tmp[1] = 4; > unset($tmp); > > which is meaningless but should work. IIRC the engine can make free's > at > the end of expression, so it shouldn't be big problem. Actually, any > assignment to it if it's not returned by-ref is meaningless, but > syntactically ok.
Last I checked, if foo() returned a string, the $tmp code above would alter the string contents... I rarely do this, but that's what I would expect it to do... But is it altering the original string, or a copy of the string or ??? You could make a ruling that this nifty new syntax is "read only" on the data, and you can't assign to it, but that's not what this naive reader would expect if this ()[] syntax is going to be supported.
-- Some people have a "gift" link here. Know what I want? I want you to buy a CD from some starving artist. http://cdbaby.com/browse/from/lynch Yeah, I get a buck. So?

Rick Widmer

19 years ago
Richard Lynch wrote:
> On Thu, November 9, 2006 11:48 pm, Stanislav Malyshev wrote: > >>>There are some cases where this could start becoming quite >>>challenging and >>>we might get into reference counting/separation problems, i.e.: >>> >>>function foo() >>>{ >>> return array(1,2,3); >>> >>>} >>>foo()[1] = 4; >> >>I'd say expression(f()[1]) means $tmp = f(); expression($tmp); >>unset($tmp); which here means: >>$tmp = foo(); >>$tmp[1] = 4; >>unset($tmp); >> >>which is meaningless but should work. IIRC the engine can make free's >>at >>the end of expression, so it shouldn't be big problem. Actually, any >>assignment to it if it's not returned by-ref is meaningless, but >>syntactically ok. > > > Last I checked, if foo() returned a string, the $tmp code above would > alter the string contents... > > I rarely do this, but that's what I would expect it to do... > > But is it altering the original string, or a copy of the string or ??? > > You could make a ruling that this nifty new syntax is "read only" on > the data, and you can't assign to it, but that's not what this naive > reader would expect if this ()[] syntax is going to be supported.
Please pardon me for butting in here, but I am having a hard time understanding this thread. I like the idea of f()[1] meaning return the [1] element of an array or character returned by f(). f(){1} should also return a character from a string. On the other hand, foo()[1] = 4; is a syntax error, plain and simple. The way I see it functions RETURN values. You can't assign to a function, so why should you be able to assign to an element of it? What am I missing?

Jasper Bryant-Greene

19 years ago
Rick Widmer wrote:
> I like the idea of f()[1] meaning return the [1] element of an array or > character returned by f(). f(){1} should also return a character from a > string. On the other hand, foo()[1] = 4; is a syntax error, plain and > simple. The way I see it functions RETURN values. You can't assign to > a function, so why should you be able to assign to an element of it? > > What am I missing?
You're missing the fact that f()[1] means the second element of the array RETURNED by f(), not the second element of f(). Therefore f()[1] = 'a'; is perfectly valid. It assigns the value 'a' to the second element of the array returned by f() -- which, by the way, might be returned by reference or might actually be an object with array-access syntax. If it's neither of these then the assignment does nothing, but so what?
-- Jasper Bryant-Greene Director Album Limited jasper@albumltd.co.nz +64 21 708 334 / 0800 425 286 http://www.albumltd.co.nz/

Richard Lynch

19 years ago
On Thu, November 9, 2006 9:13 pm, Andi Gutmans wrote:
>> >>> <?php >> >>> function foo() >> >>> { >> >>> return array(1,2,3,4,5,6); >> >>> } >> >>> >> >>> echo foo()[4]; // <---- it that >> >> It looks a bit perl-ish indeed but I don't see much trouble >> in having that it people really need it...
-0.3 :-) The thing is, I see this ending up with people starting to write unintelligible code: echo {$$(foo()[4])}()[17][13]; The 4th element of the return array from foo() is a function name which returns a 2-D array... And, honestly, if you write code like that in the first place, you ought to be shot. :-) Saving a few keystrokes is nifty, but it ends up costing far more in maintenance than its worth in this proposed syntax, imho. 'Course, I don't *have* to use it, so I just won't do that, but I'll end up having to maintain/fix somebody else's mess that DID use it, which is where the -0.3 comes from. If the code is too messed up with this kind of junk, I just won't take the job. I'd rather flip burgers than work on code like this. :-)
-- Some people have a "gift" link here. Know what I want? I want you to buy a CD from some starving artist. http://cdbaby.com/browse/from/lynch Yeah, I get a buck. So?

Mathias Bank

19 years ago
Richard Lynch schrieb:
> On Thu, November 9, 2006 9:13 pm, Andi Gutmans wrote: >>>>>> <?php >>>>>> function foo() >>>>>> { >>>>>> return array(1,2,3,4,5,6); >>>>>> } >>>>>> >>>>>> echo foo()[4]; // <---- it that >>> It looks a bit perl-ish indeed but I don't see much trouble >>> in having that it people really need it... > > -0.3 > > :-) > > The thing is, I see this ending up with people starting to write > unintelligible code: > echo {$$(foo()[4])}()[17][13]; > > The 4th element of the return array from foo() is a function name > which returns a 2-D array... > > And, honestly, if you write code like that in the first place, you > ought to be shot. :-) > > Saving a few keystrokes is nifty, but it ends up costing far more in > maintenance than its worth in this proposed syntax, imho. > > 'Course, I don't *have* to use it, so I just won't do that, but I'll > end up having to maintain/fix somebody else's mess that DID use it, > which is where the -0.3 comes from. If the code is too messed up with > this kind of junk, I just won't take the job. I'd rather flip burgers > than work on code like this. :-) >
Well, I think, a language has to support every coding. It should be the job of a good programmer to write code, that can be maintained in an easy way. That is not the job of the language. A good language supports the developer in writing clean code, it does not bind him. I like the possibilities of this syntax and I can imagine, that using this style can improve some codes (p.e. in helper-classes).

Konstantin Käfer

19 years ago
> > -0.3 > > :-) > > The thing is, I see this ending up with people starting to write > unintelligible code: > echo {$$(foo()[4])}()[17][13];
However, I could image code like: echo parse_url($url)['host']; That saves me storing the return value in a new variable and then echoing it. Konstantin Käfer – http://kkaefer.com/

Hannes Magnusson

19 years ago
On 11/23/06, Konstantin Käfer <kkaefer@gmail.com> wrote:
> > > > -0.3 > > > > :-) > > > > The thing is, I see this ending up with people starting to write > > unintelligible code: > > echo {$$(foo()[4])}()[17][13]; > > However, I could image code like: > > echo parse_url($url)['host']; > > That saves me storing the return value in a new variable and then > echoing it.
echo parse_url($url, PHP_URL_HOST); \o/ -Hannes

OURY David

19 years ago
Hello, I've just subscribed so please accept my apologies in case my question has been asked 100 times already. My company would like to contribute to the PHP native sources and share an experimental extension developed this year. The purpose of this extension is connecting to Z/OS hosts and executing IMS transactions. Who should I discuss it with? What do I have to do? Thank you for your help. Regards, David OURY david.oury@infotel.com

Johannes Schlueter

19 years ago
Hi, new extension are developed and released using PECL, see http://pecl.php.net/account-request.php for information about the process of adding software there. I like the idea of that extension and would like having it available :-) johannes On Fri, 2006-11-24 at 17:11 +0100, OURY David wrote:
> Hello, > > I've just subscribed so please accept my apologies in case my question > has been asked 100 times already. > My company would like to contribute to the PHP native sources and share > an experimental extension developed this year. > > The purpose of this extension is connecting to Z/OS hosts and executing > IMS transactions. > Who should I discuss it with? What do I have to do? > > Thank you for your help. > > Regards, > David OURY > > david.oury@infotel.com >
-- Johannes Schlüter | http://schlueters.de

Peter Hodge

19 years ago
--- Stanislav Malyshev <stas@zend.com> wrote:
> >>> <?php > >>> function foo() > >>> { > >>> return array(1,2,3,4,5,6); > >>> } > >>> > >>> echo foo()[4]; // <---- it that > > It looks a bit perl-ish indeed but I don't see much trouble in having > that it people really need it...
I think 'foo()[4]' is very intuitive (and I have wanted the feature many times myself). regards, Peter Send instant messages to your online friends http://au.messenger.yahoo.com

Ilia A.

19 years ago
While I came across instance where such a feature would be useful, I think it is a bit too Perlish for me. -0. On 9-Nov-06, at 4:21 PM, Dmitry Shirokov wrote:
> Hey guys. > > What are you thinking about adding this feature: > > <?php > function foo() > { > return array(1,2,3,4,5,6); > } > > echo foo()[4]; // <---- it that > // or may be (foo())[4] ? > > > // instead of > $var = foo(); > echo $var[4]; > ?> > > -- > Thanks, Dmitry
Ilia Alshanetsky

Marcus Börger

19 years ago
Hello Dmitry, it was requested several times in the past and i'd be all for it. The reason we didn't add it so far is that it didn't seem as important as foo()->bar() and that we had way more other things todo. best regards marcus Thursday, November 9, 2006, 10:21:45 PM, you wrote:
> Hey guys.
> What are you thinking about adding this feature:
> <?php > function foo() > { > return array(1,2,3,4,5,6); > }
> echo foo()[4]; // <---- it that > // or may be (foo())[4] ?
> // instead of > $var = foo(); > echo $var[4];
?>> Best regards, Marcus

Sean Coates

19 years ago
Hi,
> What are you thinking about adding this feature: > echo foo()[4]; // <---- it that
I'd like to see this. IMO, it's similar to $obj->objproperty->property, syntactically. It would also simplify working with third party libraries/apps that pass a lot of arrays around in a weird way *cough*gallery*cough*. S

Jan Schneider

19 years ago
Zitat von Dmitry Shirokov <deadrunk@gmail.com>:
> Hey guys. > > What are you thinking about adding this feature: > > <?php > function foo() > { > return array(1,2,3,4,5,6); > } > > echo foo()[4]; // <---- it that > // or may be (foo())[4] ? > > > // instead of > $var = foo(); > echo $var[4]; > ?>
Yes, please, that would be most important shorthand functionality after dereferencing. Jan.
-- Do you need professional PHP or Horde consulting? http://horde.org/consulting/

Sebastian Bergmann

19 years ago
Dmitry Shirokov wrote:
> <?php > function foo() > { > return array(1,2,3,4,5,6); > } > > echo foo()[4]; // <---- it that > // or may be (foo())[4] ? > > > // instead of > $var = foo(); > echo $var[4]; > ?>
Although I am not a huge fan of method chaining in general (see [1]), I am happy to have it in PHP as it faciliates fluent interfaces [2]. I makes IMHO perfect sense to also allow a similar syntax for arrays. But the implementation should be closed (in the mathematical sense), so that $o->m()[0]->n() also works, for instance, when m() returns an array that has an object with method n() at index 0.
-- [1] http://en.wikipedia.org/wiki/Law_of_Demeter [2] http://www.martinfowler.com/bliki/FluentInterface.html -- Sebastian Bergmann http://sebastian-bergmann.de/ GnuPG Key: 0xB85B5D69 / 27A7 2B14 09E4 98CD 6277 0E5B 6867 C514 B85B 5D69

Alain Williams

19 years ago
On Fri, Nov 10, 2006 at 12:55:45PM +0100, Sebastian Bergmann wrote:
> Dmitry Shirokov wrote: > > <?php > > function foo() > > { > > return array(1,2,3,4,5,6); > > } > > > > echo foo()[4]; // <---- it that > > // or may be (foo())[4] ? > > > > > > // instead of > > $var = foo(); > > echo $var[4]; > > ?> > > Although I am not a huge fan of method chaining in general (see [1]), > I am happy to have it in PHP as it faciliates fluent interfaces [2]. > > I makes IMHO perfect sense to also allow a similar syntax for arrays. > But the implementation should be closed (in the mathematical sense), so > that $o->m()[0]->n() also works, for instance, when m() returns an > array that has an object with method n() at index 0.
Also constants, eg: $numDays = array(31,28,31,30,31,30,31,31,30,31,30,31)[$month]; Any expression has type & value and you should be able to apply any operator that is appropriate to that type regardless of where the value comes from, be that: variable, literal, function, expression. So all of the following should work: $monthLength = array(31,28,31,30,31,30,31,31,30,31,30,31); $numDays = $monthLength[$month]; $numDays = array(31,28,31,30,31,30,31,31,30,31,30,31)[$month]; $numDays = array_merge(array(31,28,31,30,31,30), array(31,31,30,31,30,31))[$month]; $numDays = $months->lengths[$month]; Only the first one currently does. Expressions should also work, thus: $f = ('abcd' . 'efgh'){5}; I looked up the precedence table ( http://www.php.net/manual/en/language.operators.php ) to see if I would need parenthesis in $months->lengths[$month] (perhaps ($months->lengths)[$month]) but -> is not listed as an operator in the precedence table. Please can someone put it in!
> -- > [1] http://en.wikipedia.org/wiki/Law_of_Demeter > [2] http://www.martinfowler.com/bliki/FluentInterface.html
-- Alain Williams Parliament Hill Computers Ltd. Linux Consultant - Mail systems, Web sites, Networking, Programmer, IT Lecturer. +44 (0) 787 668 0256 http://www.phcomp.co.uk/ #include <std_disclaimer.h>

Antony Dovgal

19 years ago
On 11/10/2006 12:21 AM, Dmitry Shirokov wrote:
> Hey guys. > > What are you thinking about adding this feature: > > <?php > function foo() > { > return array(1,2,3,4,5,6); > } > > echo foo()[4]; // <---- it that > // or may be (foo())[4] ? > > > // instead of > $var = foo(); > echo $var[4]; > ?>
I would really like to see everybody spending more time on existing problems and bugs instead of adding more and more doubtfully useful features. <off> There are not more than 3 active members of the community which help developers to deal with the bug reports and in the same time there are tens of internal@ subscribers which are asking to add something. Let's reach certain level of stability first and then start to add more features. I believe as soon as the number of open reports reaches some moderate size, developers might start looking at the feature requests. And every community member can quicken his beloved feature appearance even without writing a patch: - send us new tests I can assure you that to write a small .phpt test and send it to the list would take not more time than to write a feature request. - help us with the bug reports There is a number of problems reported, which are not yet confirmed or cannot be reproduced by developers. Please go over the reports trying to reproduce each one and provide some more data. </off>
-- Wbr, Antony Dovgal

Marcus Börger

19 years ago
Hello Dmitry, the major problem here is that people have a wrong understanding of what it would do. Without return by reference it creates copies which not only makes assignments useless but also read code very slow. To overcome this, we would need to discuss another error message's fate/severity and live with yet another hard to understand, pretty much misleading feature. best regards marcus Thursday, November 9, 2006, 10:21:45 PM, you wrote:
> Hey guys.
> What are you thinking about adding this feature:
> <?php > function foo() > { > return array(1,2,3,4,5,6); > }
> echo foo()[4]; // <---- it that > // or may be (foo())[4] ?
> // instead of > $var = foo(); > echo $var[4];
?>> Best regards, Marcus