Re: [PHP-DEV] [RFC] Primary Constructors

[email protected] ("Larry Garfield")
Newsgroups php.internals
Message-ID <[email protected]>
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 <[email protected]> wrote:
>>> __
>>> Hello internals,
>>> 
>>> I'd like to put forward Primary Constructors <https://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
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.