Discussion Short Constructor
| From: | Dmitry Derepko | Date: | Tue, 15 Jul 2025 18:07:05 +0000 |
| Subject: | Discussion Short Constructor | ||
| Groups: | php.internals | ||
| Request: | Send a blank email to internals+get-128060@lists.php.net to get a copy of this message | ||
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:
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.
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