Re: Proposal for php7 class method return
| From: | Rowan Collins | Date: | Mon, 11 Jul 2016 21:20:39 +0000 |
| Subject: | Re: Proposal for php7 class method return | ||
| References: | 1 2 3 4 5 6 7 8 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-94481@lists.php.net to get a copy of this message | ||
On 11/07/2016 21:17, Marco Pivetta wrote:
On 11 July 2016 at 22:15, Rowan Collins <rowan.collins@gmail.com <mailto:rowan.collins@gmail.com>> wrote:Looking through the reasons given in your blog post, most of them don't really apply with a dedicated operator, because they're about the *contract*, and a cascade operator is only useful when the contract is to return something *other* than the current object. Specifically, it relies on the assumption that the method is not returning a new instance. To adapt one of your examples: $counter->count(); $counter->count(); $counter->count(); $counter->count(); echo $counter->getCount(); // This relies on count() mutating the current object, probably the contract is to return void The same thing, with ->> as a cascade operator: echo $counterAgain, it's just different syntax for doing things we can already do.Spot on! Plus it's something arguably considered a bad idea. I don't think that adding language features for this sort of thing is useful. Maybe it's even just harmful.
->>count()
->>count()
->>count()
->>count()
->getCount();
Or perhaps for clarity, pull the echo separately:
$counter
->>count()
->>count()
->>count()
->>count();
echo $counter->getCount();
The cascade operator is a tool to use *instead of* a fluent interface, not *with* it, and it solves a lot of the problems you outlined - $counter here can be mocked, wrapped, substituted for different implementations, etc, because the ->> operator makes no more assumptions than the code was already making.
Regards,
--
Rowan Collins
[IMSoP]