Re: [RFC] [Discussion] Add get_declared_enums() function
| From: | Rob Landers | Date: | Wed, 02 Oct 2024 08:06:20 +0000 |
| Subject: | Re: [RFC] [Discussion] Add get_declared_enums() function | ||
| References: | 1 2 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-125725@lists.php.net to get a copy of this message | ||
On Wed, Oct 2, 2024, at 09:20, Alexandru Pătrănescu wrote:
>
> On Wed, Oct 2, 2024 at 1:48 AM Juliette Reinders Folmer
> <php-internals_nospam@adviesenzo.nl> wrote:
>> L.S.,
>>
>> After the earlier discussion [1] regarding this topic in August, it is our pleasure to
>> present the RFC to add a
get_declared_enums() function to PHP for
>> discussion.
>>
>> https://wiki.php.net/rfc/get_declared_enums
>>
>> We look forward to your feedback and hope for a constructive discussion.
>
> I'm all for get_declared_enums().
> However, I don't accept that enums are not classes and going with the assumption that they
> are not will bring more problems than it solves.
>
> I noticed how you analyzed things internally, that everything is a class internally, including
> an interface or a trait.
> But this is an API for the userland, and in userland this is not true.
> The 3 structures: classes, interfaces and traits and distinct things that you can use in
> different ways in (generated) code.
>
> I believe that in userland, the common understanding is that enums are just a specific type of
> class, final ones.
> I would find it surprising if get_declared_classes() would not
> return enums.
>
> Avoiding a BC break would be nice also if there is no reason for it.
> One would be able to get only the enums by using something like:
> ```php
> function get_declared_enums_only() {
> return array_diff(get_declared_classes(), get_declared_enums());
> }
> ```
>
> --
> Alex
>
Interesting observation Alexandru,
I find it weird that class_exists("MyEnum") would return true, but there is only one
symbol table—you can’t name a class and enum with the same name, so it makes some sense.
However, while enums have “some” similarities with classes in PHP, they are not the same. For
example, you cannot implement arbitrary interfaces (ie, Stringable), you cannot extend another
class, you cannot instantiate them with “new”, you cannot have state, you cannot clone them,
magic methods are forbidden, etc.
You cannot build a “class” with these rules in PHP, so I don’t think enums quack sufficiently
like classes to be called a class. I would argue that it is an object, however. I think it would be
worth implementing a get_declared_objects() that behaves like get_declared_classes() currently does.
— Rob