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`?

Used in a (very simplistic) example:

function autoload($class) {
   $startingFrom = get_declared_class_count();
   if (!file_exists("src/{$class}.php")) {
     return;
   }
   include_once "src/{$class}.php";
   foreach (get_declared_classes($startingFrom) as $class) {
     if (!method_exists($class,'init')) {
       continue;
     }
     $class::init();
   }
}

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


> On Aug 16, 2024, at 7:13 AM, Christoph M. Becker <cmbecke...@gmx.de> wrote:
> 
> On 15.08.2024 at 03:51, Juliette Reinders Folmer wrote:
> 
>> Should a `get_declared_enums()` function be added ?
> 
> Here we go:
> 
>    function get_declared_enums() {
>        $enums = [];
>        $exts = get_loaded_extensions(false);
>        foreach ($exts as $ext) {
>            $re = new ReflectionExtension($ext);
>            $classes = $re->getClasses();
>            foreach ($classes as $class) {
>                if ($class->isEnum()) {
>                    $enums[] = $class->name;
>                }
>            }
>        }
>        return $enums;
>    }
> 
> Porting this to C is left as an excercise for the reader. ;)  Hint:
> <https://github.com/php/php-src/blob/8853cf3ae950a1658054f286117bc8f77f724f00/Zend/zend_builtin_functions.c#L1371-L1399>
> 
> Note that the terminating folding marker is backwards.
> 
>> And should the `get_declared_classes()` function be adjusted to exclude
>> enums ?
> 
> For reasons that have been stated elsewhere in this thread, I don't
> think so.
> 
> Cheers,
> Christoph

Reply via email to