Re: Discussion Short Constructor
| From: | Larry Garfield | Date: | Tue, 15 Jul 2025 19:33:03 +0000 |
| Subject: | Re: Discussion Short Constructor | ||
| References: | 1 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-128066@lists.php.net to get a copy of this message | ||
On Tue, Jul 15, 2025, at 1:07 PM, Dmitry Derepko wrote:
> Hi,
>
> I've found a discussion about Records
> https://externals.io/message/125975 and found a one key point
> which I
> really like in Kotlin (hello): short constructors.
>
> Rob said that short constructor will be probably removed:
>> 1. 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.
>
>
> Whether it's true or not I've played with syntax analyser and created a
> sugared polyfill for short constructors:
> https://github.com/php/php-src/pull/19133
>
> Many examples of how it works inside the PR, just read them.
>
> Should I propose the RFC or not?
>
> This step move us further to "light" structures or "heavy" structures
> written "simple":
>
> class RedBox extends Box(width: 50, height: 200);
>
> $box = new RedBox();
>
> instead of
>
> class RedBox extends Box {
> public function __construct()
> {
> parent::__construct(width: 50, height: 200);
> }
> }
>
> OR
>
> class RedBox extends Box {
> public function getWidth(): int
> {
> return 50;
> }
>
> public function getHeight(): int
> {
> return 200;
> }
> }
>
> OR even with Single Expression Functions RFC:
>
>
> class RedBox extends Box {
> public function getWidth(): int => 50;
>
> public function getHeight(): int => 200;
> }
>
>
> --
> Best regards,
> Dmitrii Derepko.
> @xepozz
I have some concerns that there may be edge cases we've not thought about, but at the moment I
am generally in favor. An RFC and implementation would likely help ferret out those edge cases.
I agree that this combined with single expression functions would go a long way to making
lightweight data structures even easier to create and use.
--Larry Garfield