Re: RFC: Records
| From: | Nick | Date: | Thu, 17 Jul 2025 04:33:12 +0000 |
| Subject: | Re: RFC: Records | ||
| References: | 1 2 3 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-128091@lists.php.net to get a copy of this message | ||
> On 15. Jul 2025, at 01:22, Rob Landers <rob@bottled.codes> wrote:
>
>> 5. Inline constructor isn’t necessary and could be proposed separately. I’ve thought
>> recently about this feature
>
> I will probably remove this, to be honest. It was for nesting records inside classes or other
> records, but this feature was declined, so there isn't really a need for it.
Hey Rob,
Personally, I was really excited to see inline constructors in the proposal. And I would go even
further than what you proposed.
While you already work on something completely new, why not drop
__construct
completely?
Dreaming out loud…
```php
record User(string $name, string $emailAddress) => (
// init constructor start
if (!is_valid_email($emailAddress)) { // doesn't require $this
throw new InvalidArgumentException('Invalid email address');
}
$this->id = hash('sha256', $emailAddress);
$this->name = ucwords($name);
// all properties are now immutable
) {
// actual body with everything else
public string $id;
}
```
With that:
- Both constructors would be kept together at the top; not an additional constructor potentially
after some properties and maybe methods.
- An extra level of indentation is avoided because there is no __construct around it.
- Reading doesn’t required $this for $name and
$emailAddress because we are not in an argument-free traditional constructor.
I think the same about the proposed Struct RFC, fwiw. Both care not classes.
So, why not make them more lean than classes, and feel clearly different to classes?
--
Cheers,
Nick