Re: [RFC] Primary Constructors

From: Date: Mon, 29 Jun 2026 16:40:29 +0000
Subject: Re: [RFC] Primary Constructors
References: 1 2 3  Groups: php.internals 
Request: Send a blank email to internals+get-131591@lists.php.net to get a copy of this message
On Mon, Jun 29, 2026, at 5:52 AM, Rob Landers wrote: > On Mon, Jun 29, 2026, at 12:27, Lynn wrote: >> >> >> On Mon, Jun 29, 2026 at 2:20 AM Rob Landers <rob@getswytch.com> wrote: >>> __ >>> Hello internals, >>> >>> I'd like to put forward Primary Constructors <²oӷ� >>> >÷\ÐS¢e8https://wiki.php.net/rfc/primary-constructors> for comment. >>> >>> An implementation PR will be opened later today (UTC), and the RFC updated with this >>> discussion thread. >>> >>> — Rob >> >> I don't see any mention of anonymous classes, I assume they aren't compatible >> syntax wise? > > Hi Lynn, > > That's a good observation! Originally, I had a whole section and > decided to strip it, then forgot to add it back as non-supported and > future scope. 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) {} > } > > There are a lot of caveats, which is why I dropped it for future scope: > 1. it would require types on the input parameters to disambiguate > between arguments and properties > 2. it gets weird if you don't specify a default value for the property > and makes it ambiguous as a constructor > 3. which begs the question of always having a default > In essence, it deserves its own RFC because there is a lot of subtlety > and only makes sense in the context that primary constructors exist in > the first place. > > I'll address it in the RFC, thanks! > > — Rob For anon classes, I would think the best approach is to have a compact, non-redundant syntax for "take this value from current scope and assign it to a property in this class." Basically combining promotion and closure. Doing that right now is horribly ugly. That may well be out of scope for this RFC, and that's fine, but I wanted to put that out there. --Larry Garfield

« previous php.internals (#131591) next »