On 7 February 2018 at 16:56, Andrey Andreev <n...@devilix.net> wrote:

> Hi,
>
> What's not obvious (to me at least) here is why is the current
> behavior preventing a ("sensible", as the RFC desribes it)
> implementation of function autoloading? That seems to be a major
> motivation factor for the proposal, yet doesn't seem to be explained
> anywhere.
>


It's not been mentioned explicitly in this thread, but has come up a lot in
previous discussions. I'll attempt to summarise my understanding.

Currently, when you write "foo()" in code marked as namespace "Bob", the
engine does this:

1. Check for a function "Bob\foo", use it if defined
2. Check for a function "foo", use it if defined
3. Raise an error if neither is defined

If we add autoloading of functions, the logical sequence would be to
attempt the autoloader every time we encounter something not defined:

1. Check for a function "Bob\foo", use it if defined
1a. Run autoloader callback for "Bob\foo"; use function if one is now
defined
2. Check for a function "foo", use it if defined
2a. Run autoloader callback for "foo"; use function if one is now defined
3. Raise an error if neither is defined

The problem is that all of this has to happen *every time you call the
function*, because at any time, the function could be registered, or a new
autoloader registered that knows where to find it.

If "foo" is actually a global function, that means steps 1 and 1a will be
run every time your use of "foo()" is reached, meaning that every call has
an additional overhead of calling the autoloader callback.

Since the autoloader callback might in fact be a whole stack of registered
closures, this means a significant overhead every time you mention any
global function, which includes 90% of PHP's standard library.

I hope that clarifies the general problem (and that I haven't made any
mistakes in my explanation).

Regards,
-- 
Rowan Collins
[IMSoP]

Reply via email to