Re: Primary Constructors

From: Date: Mon, 22 Jun 2026 10:38:55 +0000
Subject: Re: Primary Constructors
References: 1 2  Groups: php.internals 
Request: Send a blank email to internals+get-131449@lists.php.net to get a copy of this message
On 22 June 2026 08:27:46 BST, Nick Sdot <php@nicksdot.dev> wrote: > >```php >readonly class Point(public int $x, int $id = 0) => { // still allows hooks without readonly >   // normal constructor body behaviour >} extends Base($id) { >  // class body >} >``` > >Same as in your proposal it's just sugar. I'd expect it to behave the exact same as a >normal __construct body, and the => >{} to be optional. >As mentioned above, the sole benefit for me would be to have class construction at the very top, >co-located with the class definition itself. >You open a class -> you know what it consumes, and how it is constructed. Would be awesome! I don't think we should add extra syntax to the language just to change people's habits. If you want a constructor body as the first thing in the class, you can do that right now. In your proposed syntax, it puts an arbitrary amount of code between the class name and the "extends" and "implements" clauses, which seems to go against the aim. We could just let the "extends" clause include parameters from a separate constructor: ```php readonly class Point extends Base($id) { public function __construct(public int $x, int $id = 0) { // normal constructor body behaviour }   // class body } ``` That does raise a question of sequencing though: it implies the parent constructor is run automatically, but is that *before* or *after* the child constructor? On the whole, just making the new syntax "all or nothing" does seem a safer option. Regards, Rowan Tommins [IMSoP]

« previous php.internals (#131449) next »