Re: Should there be a `get_declared_enums()` function ?
| From: | Claude Pache | Date: | Sat, 17 Aug 2024 17:31:40 +0000 |
| Subject: | Re: Should there be a `get_declared_enums()` function ? | ||
| References: | 1 2 3 4 5 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-125028@lists.php.net to get a copy of this message | ||
> Le 17 août 2024 à 15:17, Larry Garfield <larry@garfieldtech.com> a écrit :
>
> On Fri, Aug 16, 2024, at 7:53 PM, Juliette Reinders Folmer wrote:
>> On 16-8-2024 17:01, Ayesh Karunaratne wrote:
>>> I went ahead and created PR
>>> https://github.com/php/php-src/pull/15443 along with tests, UPGRADE
>>> notice, etc. I think having a
get_declared_enums function will be
>>> helpful. The implementation is simple and straightforward too.
>> Thanks for creating the PR Ayesh!
>>
>> I'm presuming it's too late for PHP 8.4, what with feature freeze
>> having come & gone this week.
>>
>> Based on the mostly supportive responses on the list, I wonder whether
>> an RFC is needed. If so, I'd be happy to create an initial draft (for
>> PHP 8.5).
>>
>> Smile,
>> Juliette
>
> I would prefer to have an RFC for this, even if it's short. I'm not against it, I
> just think there's enough non-trivial questions (eg, impact on get_declared_classes()) that it
> warrants an RFC process/discussion.
>
> --Larry Garfield
Hi,
I concur with Larry, it should not be rushed into PHP 8.4, but carefully considered if it is really
the correct function to define. Enums were introduced three years ago, and until three days ago,
there has been apparently no complaint of the missing function, and it has been proposed less
because of a need than because of an apparent lack of symmetry with other
get_declared_*() functions. So, there is no urgency.
Now, the proposed design for get_declared_enums() is problematic, because it will bring
(or sustain) confusion. An enum is (imo rightfully) considered as a class for the purpose of
class_exists(...); and following the same logic, the proposed
get_declared_enums() returns (rightfully) a subset of the names returned by
get_declared_classes(). On the other hand, in the mind of many people – as it can be
seen in several messages in this thread, and citing the original poster –, there are four
different OO structures in PHP: classes, interfaces, traits, and enums. For example, one is tempted
to write naïvely:
```php
function get_declared_symbols(): array {
return [ ...get_declared_classes(), ...get_declared_interfaces(), ....get_declared_traits(),
...get_declared_enums() ];
}
```
not realising that enums will appear twice in the returned list.
An alternative design that is less confusing could be the following. I am not really proposing it
(as I am personally not convinced that get_declared_classes() filtered by
enum_exists() is not sufficient), but I merely give it as an illustrative example of an
api thought to avoid or reduce confusion. We could add an optional parameter to the existing
get_declared_classes() function, specifying what sort of classes they are interested
in:
```php
const SIMPLE_CLASS = 1;
const ABSTRACT_CLASS = 2;
const ANONYMOUS_CLASS = 4;
const ENUM = 8;
function get_declared_classes(int $type = SIMPLE_CLASS | ABSTRACT_CLASS | ANONYMOUS_CLASS | ENUM):
array { /* ... */ }
```
—Claude