Re: Primary Constructors
| From: | Nick Sdot | Date: | Mon, 22 Jun 2026 13:12:59 +0000 |
| Subject: | Re: Primary Constructors | ||
| References: | 1 2 3 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-131451@lists.php.net to get a copy of this message | ||
On 22.06.26 17:38, Rowan Tommins [IMSoP] wrote:
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:
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]
Hey Rowan,
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.I do not have any strong feelings on how it should look like. Let's say it would be:
readonly class Point(public int $x, int $id = 0) extends Base($id) implements Foo {
// normal constructor body behaviour
} => {
// class body
}
Also fine. Beautiful, in fact.
The before/after question is general and not exclusive to what I asked, I believe? But yes, I also thought about it. There are multiple options, I have two for now: A)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? According to Robs proposal__constructis disallowed.
Base($id) will not work in combination with => {} (or {} =>); parent controller would need to be called as in __construct
B) Control it via Base($id) and Base($this->id)
But I guess we would first need to hear from Rob how it is supposed to work without my addition.
On the whole, just making the new syntax "all or nothing" does seem a safer option.
But how to solve the readonly issue? I think if we have a chance for some new neat syntax -- and solve a problem with it, why not?
---
Cheers
Nick