Re: RFC: autoconst
| From: | Rowan Tommins | Date: | Sun, 01 Aug 2021 18:58:06 +0000 |
| Subject: | Re: RFC: autoconst | ||
| References: | 1 2 3 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-115621@lists.php.net to get a copy of this message | ||
On 01/08/2021 16:59, Serhii Smirnov wrote:
class Features {That looks like it falls into my first category: the actual value of the constant doesn't matter, you just need to be able to compare against it later. That makes it a perfect use case for PHP 8.1's new "enum" type, where you'll be able to declare this: enum Feature {const FEATURE_FLAG_WITH_SOME_LONG_NAME = 'feature_flag_with_some_long_name'; const FEATURE_SOME_ANOTHER_COOL_FEATURE = 'feature_some_another_cool_feature';}
case FEATURE_FLAG_WITH_SOME_LONG_NAME; case FEATURE_SOME_ANOTHER_COOL_FEATURE;} You can then even define the "isEnabled" function so that it only accepts Feature values, not strings (i.e. "public function isEnabled(Feature $feature): bool { ... }" instead of "public function isEnabled(string $feature): bool { ... }"). If you're not familiar with the new enums, the best documentation at the moment is probably the RFC which proposed them: https://wiki.php.net/rfc/enumerations I'm really looking forward to using them :) Regards, -- Rowan Tommins [IMSoP]