> On Aug 17, 2024, at 1:36 AM, Juliette Reinders Folmer 
> <php-internals_nos...@adviesenzo.nl> wrote:
>> What if instead PHP were to implement an optional 2nd callback parameter to 
>> `include()` / `require()` / `include_once()` / `require_once()` to allow us 
>> to capture the symbols loaded and their paths? The callback function could 
>> return `void` and accept an array of `$symbols` with the following 
>> guaranteed minimum structure?
>> 
>> $symbols = array(
>>    'classes' => [],
>>    'interfaces' => [],
>>    'traits' => [],
>>    'enums' => [],
>> );
>> 
> Except `include()` and friends aren't function calls, but language 
> constructs/expressions and don't take parameters as such, so I don't see how 
> that would be possible without changing `include()` and friends to function 
> calls (along the lines of what happened for `exit` in PHP 8.4 with 
> non-parenthesized use still possible to mitigate the otherwise huge breaking 
> change),

I know that those functions can be called as a function and return a value like 
the following:

$return_value = include($path);

I was making an assumption that since they be called like a function they could 
also accept optional parameters, although I admittedly was only assuming and do 
not know for sure.  Do you know for a fact that they cannot take a 2nd 
parameter, or just assuming like I was?   

Does anyone else know for certain it is would be possible for `include()` 
et.al. to accept an optional 2nd parameter?

However, even if a 2nd parameter is not a viable option then this could be 
addressed by instead adding  a single new function with a signature of 
`spl_autoload_callback($fn callback):callback` where the callback has a 
signature of `function ($symbols array)` which could be used like so:

function loadFile($path)
{
   if (strpos(__DIR__, 'phar://') !== 0) {
     $path = realpath($path);
     if ($path === false) {
       return false;
     }
   }
   if (isset(self::$loadedClasses[$path]) === true) {
     return self::$loadedClasses[$path];
   }
   $prior_callback = spl_autoload_callback(function($symbols) use ($path) {
     foreach ($symbols['classes'] as $className) {
       self::$loadedClasses[$path]    = $className;
       self::$loadedFiles[$className] = $path;
     }
   });
   include($path);
   spl_autoload_callback($prior_callback);
   return self::$loadedClasses[$path];
}

>> This approach would be a less disruptive than my prior suggestion
> Not so sure about that considering the above ;-)

Well then I think a single new function would be very low on the disruption 
scale, no?

> This probably needs some more bike shedding ;-)

Shedding of bike undertaken. :-)

-Mike

Reply via email to