Re: Re: Function autoloading

From: Date: Mon, 02 Sep 2013 12:58:28 +0000
Subject: Re: Re: Function autoloading
References: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17  Groups: php.internals 
Request: Send a blank email to [email protected] to get a copy of this message
Stas,

> namespace foo {
> >     use function biz\buz;
> >     use foo\bar;
> >
> >     something(); // autoloaded as "something"
>
> Wait, so it wouldn't work like class autoloader, using fully qualified
> name? And autoloader would not then have any information about
> namespaces, so you'd have to specify explicit imports or full namespace
> names for any namespaced functions you use in your code?
>

It indeed does work like the class loader in all but one case. When the
compiler knows what you're intending (via explicit use, relative or
absolute qualification), it can load the namespaced function.

The time it is different is when you don't use function and just write
foo(). With classes, this works fine, because there is no "fallback"
behavior. With functions, it can't, because it was decided long ago to
silently fall back to using a global function if this happens.

So the only case this effects is that you can't autoload a function from
the same namespace that you're currently in without explicitly using that
function.

That's not such a huge issue.

The bigger issue (for me) is that in 5.3+, you have code that means
different things depending on the state of the engine. In the example
above, that function call can mean "foo\something" or "something".

Having a function call that isn't obviously resolvable is...

But "fixing" that would be such a massive BC break...


> That doesn't sound very practical. It's much easier to do one require
> than to import manually each function in the namespace and requiring to
> explicitly name all of them removes half of the usefulness of namespaces
> - if you wanted all the full names, you could use
> very_long_names_like_this() as before.
>

You would only need to explicitly use function those in your current
namespace. Any outside (relative or absolute calls) would still work fine.

And after all, this is the exact reason we implemented use function.
Because namespaces WERE useless for functions until last week. Now we have
the power to be explicit with our calls.


> I think we discussed all these problems last time function autoloading
> topic came up.
>
> Also it is weird that namespace name is added to bar\baz() but not to
> bar(). This is contrary to how namespaces work in other places - where
> namespace name is added to everything not fully quailified.
>

That's how functions (and namespaces) have always worked. Give it a try:
http://3v4l.org/S6FN8 Keep in mind, functions have been treated
as second
class citizens in PHP since 5.3. They haven't played well with namespaces,
or autoloading. Today we've fixed half of that issue. This RFC attempts to
fix the other half.


> > Does that make sense?
>
> Frankly, for me it doesn't. Now I recall namespaces resolution and
> fallbacks was one of the reasons why function autoloading proposal
> failed the last time. IMHO this is a pretty big problem for practical
> usage of this - as pretty much the only case for function autoloading is
> resting on namespaces, so if autoloader can't deal with namespaced
> resolution rules, it's not good.


The big difference is that last time there was no mechanism to be explicit
with what you wanted your code to do without fully qualifying every call.
Today we have use function.

Anthony


Thread (72 messages)

« previous php.internals (#68850) next »