On Fri, 16 Aug 2024 at 04:37, Nick Lockheart <li...@ageofdream.com> wrote:
> Hi, > > Is there any interest in having enums as class constants? > > I'm often finding cases where I would like to have an enum inside of a > class, but don't want a free-floating enum that's basically like > another class. > > When dealing with state, it's nice to have a human readable const to > represent that state, but I always feel like they should be grouped > together. > > For example: > > class SSHClient { > > public const COMMAND_RESULT_SUCCESS = 0; > public const COMMAND_RESULT_FAILURE = 1; > public const COMMAND_RESULT_UNKNOWN = 2; > public const COMMAND_RESULT_TIMEOUT = 3; > > // ... > > } > > These constants would make sense as an enum, but they make no sense > outside of the SSHClient class that uses them. > > It seems that enums would be useful as class constants. There's a lot > of cases where a class implements a state machine and needs statuses, > but those status flags should be local to the class, not shared between > classes. > > Example: > > class SSHClient { > > public const enum CommandResult > { > case Success; > case Failure; > case Unknown; > case Timeout; > } > > // ... > } > > > // Usage: > > SSHClient::CommandResult::Success > As a heavy Enum users, I'd love this. Other said about going all the way as embedded classes, but I would voice the opinion that it's gonna feature creep like crazy and probably has a lot more work in it than anyone imagines at the moment - just look on how long it took work out aviz. Enums are very limited as classes go, so it would be probably far easier to implement them. If implementation is done in a way where it's easy to expand in the future, I don't see a need to delay for a bigger scope. That being said, I do have to ask about having methods and being able to implement an interface and add traits to the embedded enum? All of my enums implement this interface ``` <?php declare(strict_types=1); namespace App\Interface; use BackedEnum; use Symfony\Contracts\Translation\TranslatableInterface; interface TranslatableEnumInterface extends TranslatableInterface, BackedEnum {} ``` and subsequently look like this: ``` <?php declare(strict_types=1); namespace App\Enum; use App\Interface\TranslatableEnumInterface; use Symfony\Contracts\Translation\TranslatorInterface; enum ChannelTypeEnum: int implements TranslatableEnumInterface { case EMAIL = 1; case WHATSAPP = 2; case BOTH = 3; public function trans(TranslatorInterface $translator, string $locale = null): string { return match ($this) { self::EMAIL => $translator->trans('Email', locale: $locale), self::WHATSAPP => $translator->trans('Whatsapp', domain: 'non_translatable', locale: $locale), self::BOTH => $translator->trans('Both', locale: $locale), }; } } ``` Honestly, I would really like to do this right there in class embedded, as having a separate file really is overkill :) -- Arvīds Godjuks +371 26 851 664 arvids.godj...@gmail.com Telegram: @psihius https://t.me/psihius