Re: Function auto-loading

From: Date: Mon, 08 Aug 2016 17:54:04 +0000
Subject: Re: Function auto-loading
References: 1 2 3 4 5 6 7  Groups: php.internals 
Request: Send a blank email to internals+get-94946@lists.php.net to get a copy of this message
On 08/08/2016 18:03, Levi Morrison wrote:
    If not, I don't see why we ever need to be able to autoload global
    functions. "You want autoloading? Put it in a namespace." Like I
    say, that leaves the very small edge case of a single namespace
    spanning multiple files, and an autoloader implementation able to
    include one of them when a function is called from another.
I'm not sure why you would think a single namespace spanning multiple files is a "very small edge case". I disagree. Here are some libraries I am aware of *off the top of my head* that use functions the same namespace across multiple files: * https://github.com/nikic/iter * https://github.com/lstrojny/functional-php As well as several of my personal projects. I do not think this is a "very small edge case."
The "iter" example looks a long way from being autoloadable whatever we supported, but the example of one-function-per-file is definitely relevant, so I stand corrected. After a bit of clicking, I even managed to find a line which would fail to autoload under the proposed limitation: https://github.com/lstrojny/functional-php/blob/master/src/Functional/CompareObjectHashOn.php
return compare_on($comparison, $keyFunction);
Although interestingly, at the top of the file there is a (technically unnecessary) "use function Functional\compose;" If there was a "use function Functional\compare_on;" as well, we'd be fine. (The function name would then become qualified at compile time and trigger autoloading at run time.) On 08/08/2016 18:06, Rasmus Schultz wrote:
Unless there's a demonstrated, critical performance issue with auto-loading of global functions, please, let's not cripple this feature with inconsistencies from the get-go!
Sure, we could try to measure it, but remember that it's not just the engine code that has the extra cost, it will actually call a userland function every time you use a global function from inside a namespace if you don't add a leading "\". That userland function will probably do a bunch of string comparisons before deciding it's not interested, and may even try to stat a file or two. Those are really expensive operations, so I think it's a long way from "micro-optimisation". Unfortunately, function name resolution has this quirk that class name resolution doesn't, so something's got to give. Regards, -- Rowan Collins [IMSoP]

« previous php.internals (#94946) next »