On Fri, Jul 3, 2026 at 4:15 AM Alex Rock <pierstoval@gmail.com> wrote:
Le 02/07/2026 à 16:41, Michael Morris a écrit :
This is tangential to the function autoload discussion.
An idea that popped into my head an hour ago, that I don’t have another PHP Dev to talk to about, what if you could register a function that is called by PHP the first time it sees a namespace declared?
This would allow functions and for that matter constants of the namespace to be declared before they get called. It isn’t as clean as waiting until the function call itself is being called, but maybe that’s for the best. After all, if you have a dozen 10 line utility functions do you necessarily want 10 files.
Again, I don’t know the engine, but this doesn’t seem like this would be that hard to do. Note that how autoloaders register themselves to this function, resolve what to load and so on is a separate can of worms.
As to what to call it, maybe spl_namespace_register($callback, $prepend). The callback gets the string of the namespace that PHP just saw for the first time and it gets to execute before any code in that namespace gets to execute.
Thoughts?
Interesting idea, not sure whether it’s viable or not.
As of how I understand it, there are multiple ways to do this and all of them will imply different ways developers can use such feature.
With the namespace order, for instance, imagine the very first namespace declaration is “namespace Foo\Bar\Baz;”: what is the order of declarations that will be sent to callbacks?
- Will the callbacks receive “Foo\Bar\Baz” as argument directly and be called only once?
Hmm.. Good question. I think one call, with Foo\Bar\Baz, because the goal is to have a hook so user code can initiate the referencing of that name space. It would be up to the callback to sort out what to do - the engine need only give opportunity.
And this applies for use statements as well. For example, if use A\foo; is foo a class? a function, a constant? Who knows, but this is the moment to find out and execute this handler.
Or, perhaps, change the use statement so that it explicitly fires a callback if the referenced chain doesn’t already exist. That, however, creates some overlap to spl_autoload_register, but it might be cleaner all the same. Calling in functions of a namespace could get JavaScript levels of obnoxious and runs against the grain of PHP, but this could be eased with wildcard loading use A\* telling the callback to load all symbols related to A explicitly.
Maybe this should be tied to use and not to namespace entirely? No - I don’t think so, this isn’t intuitive
namespace A;
use A/*;
So I’m thinking both will be needed.
- Will they be called once per sub-namespace? If yes, will it be like calling the callback three times, like this:
$callback('Foo'); $callback('Foo\\Bar'); $callback('Foo\\Bar\\Baz');
- How should it propagate if other sub-namespaces of these namespaces are declared? Like, if you have statements like “namespace Foo\Bar {} namespace Foo\Baz {}” , would it call registered callbacks like “Foo → Foo\Bar → Foo\Baz”, or should it do like no1 and just go with “Foo\Bar → Foo\Baz”?
- Or something else?
I think some coordination with the engine will be needed, not necessarily required but for optimization definitely needed.
The engine will track which namespaces it has given the callback the opportunity to set up. The callback, by returning an array, can tell the engine what namespaces it has set up. So on namespace A\B\C; The callback could return [‘A’, ‘A\B’, ‘A\B\C’, ‘A\D’, ‘A\D\E’] because it knows those other sub namespaces exist and doesn’t need to be pestered about them in the future.
Use statements will need to assume the last symbol encountered is a class, constant or function and step over it. So
namespace B;
use C\foo;
use D\*;
Breaking down, the engine could end up firing the callback three times. However, when the callback is made for B it could, for whatever reason, register C and D as well, run their setups and mark them off - tell the engine it’s done that so the other two firings would be skipped.
Further thinking on the second use statement above - the callback gets namespace ‘D’ and needs to set it up. But, this is also an opportunity to change the load order. Say D has a strpos statement for whatever reason - the use D* would cause the D namespace to be checked before the root namespace. I believe the current namespace is also always checked before the root as well.
This callback could give us a logical place to tell the engine to reverse this - check root before checking local for a given namespace.
There might be other issues, but that’s my “hot reaction” when reading your suggestion 
Thanks. Again, this is an idea I haven’t really sussed out all the corner cases. It just struck me as something that could be useful and the more I look at it the more useful it seems - but symbol loading is a vital and complicated part of the engine and it definitely needs brainstorming before any RFC to change it in any way.