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:


```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]

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.

```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?
According to Robs proposal `__construct` is disallowed.


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) `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

Reply via email to