# Feature request

- Thread: https://externals.io/message/26440
- Original archive: https://news-web.php.net/php.internals/26440
- Started by Dmitry Shirokov on 2006-11-09T21:21:45+00:00
- Messages: 12
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · RFCs at https://externals.io/rfc.md · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #26440 — Dmitry Shirokov — 2006-11-09T21:21:45+00:00

URL: https://externals.io/message/26440#26440

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

---

## #26441 — Edin Kadribasic — 2006-11-09T22:50:03+00:00

URL: https://externals.io/message/26440#26441 · In reply to: #26440

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

---

## #26442 — Evert | FileMobile — 2006-11-09T23:05:04+00:00

URL: https://externals.io/message/26440#26442 · In reply to: #26441

Edin Kadribasic wrote:
> Dmitry Shirokov wrote:
>   
> > […]
>
>
> +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

---

## #26443 — Ilia Alshanetsky — 2006-11-09T23:12:34+00:00

URL: https://externals.io/message/26440#26443 · In reply to: #26440

While I came across instance where such a feature would be useful, I  
think it is a bit too Perlish for me.

-0.




> 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

---

## #26444 — Marcus Boerger — 2006-11-09T23:14:49+00:00

URL: https://externals.io/message/26440#26444 · In reply to: #26440

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

---

## #26445 — Sean Coates — 2006-11-09T23:22:44+00:00

URL: https://externals.io/message/26440#26445 · In reply to: #26440

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

---

## #26446 — Stanislav Malyshev — 2006-11-10T01:44:16+00:00

URL: https://externals.io/message/26440#26446 · In reply to: #26442

> > […]

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/

---

## #26458 — Jan Schneider — 2006-11-10T11:06:36+00:00

URL: https://externals.io/message/26440#26458 · In reply to: #26440

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/

---

## #26460 — Sebastian Bergmann — 2006-11-10T11:55:45+00:00

URL: https://externals.io/message/26440#26460 · In reply to: #26440

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

---

## #26464 — Alain Williams — 2006-11-10T12:20:29+00:00

URL: https://externals.io/message/26440#26464 · In reply to: #26460

> Dmitry Shirokov wrote:
> > […]
> 
>  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

---

## #26471 — Antony Dovgal — 2006-11-10T16:09:23+00:00

URL: https://externals.io/message/26440#26471 · In reply to: #26440

> 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

---

## #26497 — Marcus Boerger — 2006-11-10T22:48:28+00:00

URL: https://externals.io/message/26440#26497 · In reply to: #26440

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
