On 17-8-2024 2:00, Mike Schinkel wrote:
To everyone on this thread and thus nobody in specific (hence my top post):
Given that get_declared_*() populates a new ZEND_HASH each time the function is
called[1] — which for classes can easily be hundreds of strings — would there
be any appetite to:
1. Add an optional parameter — e.g. `get_declared_classes($startingFrom)` — that can
limit the number of symbols starting with "n" where if n==100 then it would
return array_slice($symbols,100), and
2. Add a function — `get_declared_class_count()` — that returns the current number of
declared symbols of whichever kind of symbol, so we can get the "n" before
calling `include`/`require` to use with get_declared_*() after calling
`include`/`require`?
This would cut down on creating hashes with 100+ strings that get immediately
thrown away, and also cut down on memory fragmentation/garbage collector churn.
-Mike
[1]
https://github.com/php/php-src/blob/18d41502da0da1bb3928e60c41f1b821974c2c01/Zend/zend_builtin_functions.c#L1371
Mike,
While your suggestion does not directly relate to what triggered my mail
to the list, I very very much like the idea.
It's as if you are reading my mind or rather, reading one of the
autoloaders [1] I need to maintain, in which your above outlined
proposal should definitely make for more efficiency.
Having said that, the same parameter + extra function would then also be
needed for `get_declared_interfaces()`, `get_declared_traits()` - and,
if it would be added, `get_declared_enums()`.
There is one problem I can see with this approach though: since PHP 7.4,
the return value of `get_declared_classes()` (and friends) does not
guarantee any particular order anymore [2].
So, an `array_slice($symbols, $n)` may not get you the _latest_ classes
loaded, so I think this would only work if the order of classes is
guaranteed in some way.
Smile,
Juliette
1:
https://github.com/PHPCSStandards/PHP_CodeSniffer/blob/6fbbc1078094d905f0773421f13830744a144d1e/autoload.php#L153-L247
2:
https://www.php.net/manual/en/function.get-declared-classes.php#refsect1-function.get-declared-classes-changelog