Hi all,
Enums are not allowed to include object properties and with property
hooks it's now possible to define get-only dynamic properties.
I just come across the following example and wonder if this could be
allowed in the future:
enum Month:int {
case January = 1;
case February = 2;
case March = 3;
case April = 4;
case May = 5;
case June = 6;
case July = 7;
case August = 8;
case September = 9;
case October = 10;
case November = 11;
case December = 12;
public string $abbreviation {
get => substr($this->name, 3);
}
}
Means, allowing get-only property hooks on enums reducing the need to
write "old school" getter methods.
Is that something feasible and how complicated would that be to implement?
Best,
Marc Bennewitz
Hi
Am 2025-02-07 16:03, schrieb Marc Bennewitz:
Is that something feasible and how complicated would that be to
implement?
Supporting this might cause conflicts when full-blown tagged-unions are
proposed, because those would actually support properties and semantics
might need to be different than those a naive “rushed” implementation of
property hooks for enums would propose.
See https://wiki.php.net/rfc/tagged_unions for an very early draft
(which is not in a shape to be discussed further).
Best regards
Tim Düsterhus
Hi Tim,
Unintentionally, I first replied to Tim only but it was meant to be send
to the list.
Hi
Am 2025-02-07 16:03, schrieb Marc Bennewitz:
Is that something feasible and how complicated would that be to
implement?Supporting this might cause conflicts when full-blown tagged-unions
are proposed, because those would actually support properties and
semantics might need to be different than those a naive “rushed”
implementation of property hooks for enums would propose.See https://wiki.php.net/rfc/tagged_unions for an very early draft
(which is not in a shape to be discussed further).
I see - that would actually be a very nice feature - looking forward to it.
First thought it might have been overlooked and just not included for
simplicity, but yes, tagged union looks much more impressive.
Thanks for your helpful explanation!
Best regards
Tim Düsterhus
Best,
Marc Bennewitz