Re: Concept: constructor overloading syntax sugar
| From: | Alexandru Pătrănescu | Date: | Mon, 17 Feb 2025 14:58:20 +0000 |
| Subject: | Re: Concept: constructor overloading syntax sugar | ||
| References: | 1 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-126429@lists.php.net to get a copy of this message | ||
Hi Viktor,
On Mon, Feb 17, 2025 at 4:42 PM Viktor Khramov <dev.999.victor@gmail.com>
wrote:
> Hi!
>
> The point is here:
> https://gist.github.com/vhood/665418835e65be26d5a818fded92ab75
>
> Is it possible? May I create an RFC for this?
>
Already possible like this:
class Decision
{
#[Main]
public function __construct(
private readonly Uuid $id,
private readonly string $comment,
) {
}
public static function fromEmail(string $mailSubject, string
$mailBody): static
{
$id = preg_replace('/pattern/', "$1", $mailSubject);
$comment = preg_replace('/pattern/', "$1", $mailBody);
return new static(
id: new Uuid($id),
comment: $comment,
);
}
}
Related to overloading, that is binding to the correct function in Java/C#
at compile time, and IMO is not matching with the dynamic nature of PHP.
I think we're not losing much by not having this feature in PHP, but feel
free to think more about what the implementation would look like.
-- Alex