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>

-- 
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to