Re: Regarding constructions like func()[0]
| From: | jvlad | Date: | Sun, 08 Nov 2009 09:23:24 +0000 |
| Subject: | Re: Regarding constructions like func()[0] | ||
| References: | 1 2 3 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-45974@lists.php.net to get a copy of this message | ||
""melfar"" <melfar@gmail.com> wrote in message
news:0F.F6.03668.AD206FA4@pb1.pair.com...
> Hey,
>
> Yep, I think there might be some caveats to that change.
> The original patch did not work on methods, so I've fixed that and also
> made the return value an rvalue, so that you can't attempt to modify the
> temporary value, the zend_check_writable_variable guard will yield an
> error.
>
> The updated patch can be found at
> http://bugs.php.net/bug.php?id=50003
>
> I've also heard an opinion that adding this construction is just going to
> make things more
> complicated, but take this: chained -> operators are supported for return
> values and I'm only seeing code getting simpler because of that.
>
> Br,
> -melfar
- the patch introduces 5 more grammar conflicts. Did you check them all?
- can't call the results useful. Instead of returning possibly long array
and fetching just
one element, why not to add an argument to the funtion - the index of the
element
to be returned?
- if you badly need the "function" returning an array and you prefer syntax
to performance,
think about indexed class property implemented via setters/getters:
class A {
public function __get($prop) {
if ($prop == "b")
return array('a' => 'b');
}
}
$a = new A();
echo $a->b['a'];
Class instance is where you'll keep the context.
If you don't need any context, static properties would be useful, but they
can't be
implemented via setters/getters and this is a thing should be changed in
order to get
the following sample working:
<?php
class A {
public static function __get($prop) {
if ($prop == "b")
return array('a' => 'b');
}
}
echo A::$b['a'];
?>
I think php is not that bad after all :)
just 2c.