Re: [RFC] Primary Constructors

From: Date: Mon, 29 Jun 2026 12:07:32 +0000
Subject: Re: [RFC] Primary Constructors
References: 1 2 3  Groups: php.internals 
Request: Send a blank email to internals+get-131583@lists.php.net to get a copy of this message
On 29 June 2026 11:52:45 BST, Rob Landers <rob@getswytch.com> wrote: > > FWIW, I was originally going to support them with something like: > >$arg = 'foo'; >$class = new class(public int $x = $arg) {} > >Which desugars to: > >$arg = 'foo'; >$class = new class($arg) { > public function __construct(public int $x) {} >} I actually have a draft RFC kicking around for this, but using a different syntax inspired by closures: $arg = 'foo'; $class = new class() use (public int $arg) {} This allows a very concise version for very simple cases: $class = new class() use ($arg) {} I even had a working implementation, but paused it to work out if a constructor could be allowed as well. If the syntax allowed for a parent constructor call, though, that might be enough for most use cases. Although it's not obvious, anonymous classes are actually compiled once, and then constructed multiple times, which means the syntax is doing quite a lot more than constructor promotion. I agree that it should be left as future scope to pin down exactly how it should look and work. Rowan Tommins [IMSoP]

« previous php.internals (#131583) next »