On Mon, Feb 1, 2021 at 9:30 PM Larry Garfield <la...@garfieldtech.com>
wrote:

> On Mon, Feb 1, 2021, at 11:48 AM, Alexandru Pătrănescu wrote:
>
>
> > > https://wiki.php.net/rfc/enumerations
> > >
> > > At this point, Ilija and I consider the RFC done and ready for a vote.
> > > Baring any major issues being brought up, we plan to start the vote in
> the
> > > first half of next week, probably Tuesday-ish.  If you have any other
> bug
> > > reports or tweaks, please speak now or forever hold your patches.
> > >
> > > --Larry Garfield
> > >
> > >
> > Hi,
> >
> > Maybe IterableEnum can actually be just Enum. As you are not allowed to
> > implement it and/or define it, wouldn't it work? That's how it's named
> > internally in Java, not that it would matter but sometimes people forget
> > it's just syntactic sugar there as well (
> > https://docs.oracle.com/javase/8/docs/api/java/lang/Enum.html): public
> > abstract class Enum<E extends Enum<E>> implements Comparable<E>,
> > Serializable.
>
> We tried that.  The `enum` keyword precludes any class or interface being
> called `Enum`, even internally.
>
>
I see. I made an assumption based on how I thought the situation should be.
Maybe that should not be impossible but I'm a bit further from
understanding how that could be fixed.

> For storing in a database purpose, property name can be used directly, I
> > think.
> > It would nice to have in the rfc the recommended way to retrieve the
> Enum,
> > given that you know the name.
> > I'm guessing that would be Suit::$name;
>
> That doesn't work for referencing a constant; it gets read as a static
> property.  That's a more general syntactic question for PHP, and one we're
> not going to solve here. :-)  Really, ->name is more an implementation
> artifact.  If you want to have a primitive to pass around, for whatever
> reason, that's what a backed enum is for.
>
>

Yes, you are right. It would work however with constant(Suit::class . '::'
. $name) so someone could very easily define a static method for that and
serialization for a database is back for not backed enums.
Maybe there would be a way to remove the name public property completely to
avoid missusages?

So a "pure enum" should only be used in code as property, parameter, return
value types, reading from the constant and compared with identity operator
(match included).
Whenever that enum needs to be stored as a value as user defined
int/string, a backed enum should be used that has a public read-only value
property and a public static from method.
Yes... sounds good.

Alex

Reply via email to