Enum + property hook getter
| From: | Marc Bennewitz | Date: | Fri, 07 Feb 2025 15:03:15 +0000 |
| Subject: | Enum + property hook getter | ||
| Groups: | php.internals | ||
| Request: | Send a blank email to internals+get-126332@lists.php.net to get a copy of this message | ||
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 {
Attachment: [application/pgp-keys] OpenPGP public key OpenPGP_0x3936ABF753BC88CE.asc
Attachment: [application/pgp-signature] OpenPGP digital signature OpenPGP_signature.asc
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
Attachment: [application/pgp-keys] OpenPGP public key OpenPGP_0x3936ABF753BC88CE.asc
Attachment: [application/pgp-signature] OpenPGP digital signature OpenPGP_signature.asc