Re: Should there be a `get_declared_enums()` function ?

From: Date: Fri, 16 Aug 2024 05:58:02 +0000
Subject: Re: Should there be a `get_declared_enums()` function ?
References: 1 2  Groups: php.internals 
Request: Send a blank email to internals+get-124970@lists.php.net to get a copy of this message
> On Wed, Aug 14, 2024, at 8:51 PM, Juliette Reinders Folmer wrote: > Should a get_declared_enums() function be added ? My answer is "very much, yes." > On Aug 15, 2024, at 9:09 AM, Larry Garfield <larry@garfieldtech.com> wrote: > Context: I can't remember the last time I used get_declared_classes() (...it's a > kinda pointless function) Your experience is one data point. Another data point is my experience that it is fortunate that get_declared_classes() exists as I find it to be a very a useful function. The core library I use for most projects depends on it for class initialization. Your comment made me curious to see if others use it, and there are 8.1k uses on GitHub: - https://github.com/search?q=language%3Aphp+get_declared_classes&type=code Here is how I have used, in a greatly simplified example: function autoload($class) { $classCount = count(get_declared_classes()); if (!file_exists("src/{$class}.php")) { return; } include_once "src/{$class}.php"; foreach (get_declared_classes() as $class) { if (!method_exists($class,'init')) { continue; } $class::init(); } } As an aside, for this use-case I have always wanted PHP to add both 1.) a get_declared_class_count() function and 2) optional parameters for get_declared_classes() that would allow requesting a subset of classes, such as just the newly added classes. How important this is depends on if PHP maintains that array internally and just returns a reference, or if it instantiates memory and populates the array of classes to return every time the function is called. > , so when we were working on enums it never occurred to us to think about it. It wasn't a > deliberate decision to omit, as far as I recall. > > I think I'd be open to adding it; my concern would be the overlap with > get_declared_classes(), which as you note currently would include enums, and changing that is a BC > break (even if a tiny one that I doubt would impact anyone). Add another parameter — or include as part of the args array — something like this (though I am sure this could be bikeshed to be better): get_declared_classes(DeclaredClasses:ONLY_CLASSES, ['starting_from'=>174]); get_declared_classes(DeclaredClasses:ALL, ['recently_declared'=>3]); Or even: DeclaredSymbols::get_classes([$args]) <-- returns only classes, not enums DeclaredSymbols::get_enums([$args]) DeclaredSymbols::get_interfaces([$args]) DeclaredSymbols::get_traits([$args]) #fwiw > On Aug 16, 2024, at 12:23 AM, Juliette Reinders Folmer > <php-internals_nospam@adviesenzo.nl> wrote: > Thanks for the response. It was exactly a (custom) autoloader situation which caused me to > start wondering about this. Seems Juliette's use-case may be at least somewhat similar to mine. -Mike

« previous php.internals (#124970) next »