Re: [RFC] Primary Constructors
| From: | Rob Landers | Date: | Mon, 29 Jun 2026 10:52:45 +0000 |
| Subject: | Re: [RFC] Primary Constructors | ||
| References: | 1 2 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-131574@lists.php.net to get a copy of this message | ||
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
>> <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