Re: Raising the precedence of the new operator
| From: | Marco Pivetta | Date: | Mon, 05 Apr 2021 20:51:33 +0000 |
| Subject: | Re: Raising the precedence of the new operator | ||
| References: | 1 2 3 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-113969@lists.php.net to get a copy of this message | ||
On Mon, Apr 5, 2021, 19:42 Mike Schinkel <mike@newclarity.net> wrote:
>
> > On Apr 5, 2021, at 12:47 PM, Ben Ramsey <ben@benramsey.com> wrote:
> >
> >> On Apr 5, 2021, at 11:40, André Hänsel <andre@webkr.de> wrote:
> >>
> >> I was wondering... PHP is the only language I know of where you have to
> >> write
(new Foo())->bar() instead of
> >> new Foo()->bar(). This is particularly apparent
> >> with the builder
> pattern:
> >>
> >> $developer->drink((new Coffee())->withCream()->withSugar());
> >> $logger->log((new LogMessage())->withMessage('Coffee was drunk'));
> >>
> >> Since new Foo()->bar() cannot (and probably should
> >> not) be used to
> >> "dynamically instantiate a new thing of the class
> >> name returned by function Foo()", it seems like it would be no problem
> to
> >> change the precedence rules so that
> >> new Foo()->bar() means "instantiate a new Foo
> >> and call bar() on it".
> >>
> >> It's currently a syntax error, so allowing it would be automatically
> >> compatible.
> >>
> >> Has this ever been discussed before?
> >
> >
> > Mike Schinkel and I recently discussed this off-list. Mike, now’s your
> > time to chime in with your ideas on this. :-)
>
> Thanks for the invite.
>
> I had asked Ben what he thought about allowing the following syntax as a
> synonym for new Foo():
>
> Foo::new()
>
> That would be backward compatible because you currently cannot have a
> method named new().
>
I have static ctors named new in multiple codebases:
https://3v4l.org/FudWpk
>