Hi,
I'm writing this email with a reference to https://wiki.php.net/rfc/howto.
My suggestion is that all enums (both basic/pure and backed) should
implement Stringable interface by default, and before you say that it's
totally wrong or impossible let me explain why.
Let's say I have a backed enum:

enum Suit: string {
  case Hearts = 'H';
  case Diamonds = 'D';
  case Clubs = 'C';
  case Spades = 'S';}

Now if I'm trying something like this "echo Suit::Hearts;"I will get an
error "Fatal error: Uncaught Error: Object of class Suit could not be
converted to string"
BUT if I'm trying to use this enum as a part DTO (data transfer object) and
then serialize it or do something as simple as "echo
json_encode(Suit::Hearts);" then suddenly PHP knows how to convert enum to
string. This feels so unfair to me that I finally decided to write this
email.
The same applies to basic enums which have only case names, but they are
still stringable as $enum->name.

Please give me the initial feedback to understand if it's worth proceeding
with a RFC or not.


Best regards,
Janis Kajaks

Reply via email to