Autoloading namespace constants and functions
| From: | Chuck Hagenbuch | Date: | Wed, 24 Oct 2007 19:40:58 +0000 |
| Subject: | Autoloading namespace constants and functions | ||
| Groups: | php.internals | ||
| Request: | Send a blank email to internals+get-32973@lists.php.net to get a copy of this message | ||
A few different proposals have mentioned the possibility that import or use statements might trigger autoload.
My initial reaction is that this would be bad, BUT: it could let us use autoload to handle namespaced function libraries and namespace constants also - as long as they were imported. Which would be good.
The missing piece would be allowing autoload to trigger when a fully-qualified name was used whether that name was for a function, class, or constant.
In other words, I'd like autoload to kick in when I do this:
<?php
$stuff = My::Namespace::do_stuff();
?>
... but let do_stuff be a function in the My::Namespace namespace, not a class method of the Namespace class in the My namespace.
Right now as far as I can see autoload will trigger for this situation, but it will fail because after autoload finishes because My::Namespace wasn't declared as a class. Same for accessing namespace constants.
Would it be possible to, after autoload runs, go back to seeing if the call is now resolvable, rather than assuming that a class had to be loaded?
To try and fend off any "just don't use autoload" responses, the reason I want this to work is that if I can organize my libraries in a way that they are autoloadable, I can leave the decision on how code should be included up to the top-level application using the library. I can use an autoloader with development or when building a site, and then I can start including files explicitly - with no conditional includes and no _once usage - in a very opcode-cache friendly way, with no changes to the libraries or the rest of the app. I think that's a very desirable situation, and namespaces almost get us there - I just can't make full use of namespaced functions or namespace constants right now if I want to do it.
-chuck