Re: [PHP-DEV] RFC Draft - Improved Autoloading
[email protected] (Michael Morris) Tue, 7 Jul 2026 17:33:06 -0400
| Newsgroups | php.internals |
|---|---|
| Message-ID | <CAEUnE0cLgGXh6zfanoF-yB-RY5OCN16r5Dp22oKndzRsH3uC_A@mail.gmail.com> |
On Tue, Jul 7, 2026 at 4:50 PM Alex Rock <[email protected]> wrote: > Le 07/07/2026 à 05:06, Michael Morris a écrit : > > > > I'm not sure that I like the current shift of the first ideas you exposed. > > At first, it was about adding an internal hook when encountering a > "namespace" statement. Something that could be used to setup libraries > *after* autoload, for instance. > At first it was a function to hook into the namespace keyword and fire code that would initialize the newly declared namespace. However, namespaces also get declared when a new class is loaded. The namespace init logic would still end up firing - when the class def file is loaded it's going to have a namespace keyword. So this can be be accomplished with just that function - but it occured to me that it's actually highly unlikely that a dev will want to do this without also taking autoloading into account. So it makes sense to unify the handlers. > > Here, your new draft suggests an update to the "use" statement, which > currently has absolutely zero impact, and doesn't trigger autoload at > all. And you've misread the proposal entirely. Use statements still do nothing under this proposal. > Plus, it would trigger in two different ways, depending on whether > the final symbol exists in the symbols table or not, and triggering > another call with the namespace as argument. > > Again, no, that's not how it works at all. I'll repeat the example ``` namespace Acme\Foo\Widgets; ``` The autoloaders may be called after this line. Supplied callbacks with 2 arguments will receive (null, "Acme\Foo\Widgets"). Supplied callbacks expecting 1 argument will NOT BE CALLED, as they are presumed to be of the older form and are only able to load classes, and that's not what's happening here. ``` use Omnicorp\Bar\Helpers; ``` Nothing happens on this line currently, or under the RFC. ``` $controller = new Controller(); ``` The engine will look for Acme\Foo\Widgets\Controller. If it doesn't find it autoloaders are invoked. Autoloaders that expect 1 argument will receive "Acme\Foo\Widgets\Controller". Autoloaders expecting 2 arguments will receive ("Acme\Foo\Widgets\Controller", null). Why null? Because the namespace declaration at the beginning of the file already tripped off namespace initialization and it should not need to run again. ``` $name = make_widget_name(); ``` The engine will try to call Acme\Foo\Widgets\make_widget_name, and on failure will try \make_widget_name, and if that fails a fatal error is raised. This function can be defined by the namespace initialization code gets called with the namespace statement. ``` echo strlen($name); ``` The engine will try to call Acme\Foo\Widgets\strlen() and on failure will call \strlen() which is always available. ``` Helpers\spin_wheels(); ``` The use statement above silently rewrites this line as Omnicorp\Bar\Helpers, and now at this line Omnicorp\Bar\Helpers\spin_wheels is called. If it fails, since "Omnicorp\Bar\Helpers" is not in the list of initialized namespaces the engine will call autoloaders expecting 2 arguments only with argument (null, "Omnicorp\Bar\Helpers"). After these callbacks run, Omnicorp\Bar\Helpers\spin_wheels() will be retried. If it fails \spin_wheels() will be tried, and a fatal error raises if the symbol still does not resolve. I will not address the rest of your reply as it is responding to a misunderstanding of how the RFC works.