Re: [RFC] new foo()->bar()
| From: | Felipe Pena | Date: | Sat, 27 Nov 2010 17:08:30 +0000 |
| Subject: | Re: [RFC] new foo()->bar() | ||
| References: | 1 2 3 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-50625@lists.php.net to get a copy of this message | ||
Hi,
2010/11/26 Felipe Pena <felipensp@gmail.com>
> 2010/11/26 Johannes Schlüter <johannes@schlueters.de>
>
> On Fri, 2010-11-26 at 17:36 -0200, Felipe Pena wrote:
>> > var_dump(new foo()->bar()->x); // string(3) "PHP"
>>
>> It has some readability issues. One might assume it is
>>
>> new (foo()->bar()->x)
>>
>> not
>>
>> (new foo())->bar()->x
>>
>> As there is a mandatory space between "new" and its operand and no space
>> in front of the object operator and we allow non-constant operands to
>> "new".
>>
>> So what is
>>
>> new $bar->foo();
>>
>> ? If I read the patch correctly this is valid and evaluated as
>>
>> (new $bar)->foo();
>>
>> johannes
>>
>>
>
> new foo()->bar() should be read as: (new foo())->bar().
>
> And using variable:
>
> new $bar->y()->x should be read as: (new ($bar->y)())->x.
>
> <?php
>
> class foo {
> public $x = 1;
> }
>
> class bar {
> public $y = 'foo';
> }
>
> $bar = new bar;
>
> var_dump(new $bar->y()->x); // 1
>
> ?>
>
> I.e. just as it is nowdays.
>
Well, if this feature is going to be accept, we could to decide what syntax
to use.
I have created another patch which is the bracketed version of this
presented here.
<?php
class foo {
public $x = 1;
}
class bar {
public $y = 'foo';
}
$x = 'bar';
$bar = new bar;
var_dump((new bar)->y); // foo
var_dump((new $x)->y); // foo
var_dump((new $bar->y)->x); // 1
?>
Thus we do not have the readability issues, as pointed by Johannes.
http://wiki.php.net/rfc/instance-method-call
(updated!)
Thanks for the comments.
--
Regards,
Felipe Pena