# Autoloading namespace constants and functions

- Thread: https://externals.io/message/32973
- Original archive: https://news-web.php.net/php.internals/32973
- Started by Chuck Hagenbuch on 2007-10-24T19:40:58+00:00
- Messages: 2
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #32973 — Chuck Hagenbuch — 2007-10-24T19:40:58+00:00

URL: https://externals.io/message/32973#32973

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

---

## #32974 — Stanislav Malyshev — 2007-10-24T19:50:04+00:00

URL: https://externals.io/message/32973#32974 · In reply to: #32973

> A few different proposals have mentioned the possibility that import or 
> use statements might trigger autoload.

I don't think it's a good idea. Import is a compile-time name 
transformation, it doesn't even have to be a name of any real class or 
namespace.

> 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.

That won't be autoload that we have now, but entirely different 
functionality, and I'm not sure many people would really need it.

> ... but let do_stuff be a function in the My::Namespace namespace, not a 
> class method of the Namespace class in the My namespace.

And how the engine is supposed to know which one do you mean?

> 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.

You can't autoload functions and constants - just as you couldn't 
before. That doesn't prevent you from using them - just as it didn't 
prevent you from using them before :)
-- 
Stanislav Malyshev, Zend Software Architect
stas@zend.com   http://www.zend.com/
(408)253-8829   MSN: stas@zend.com
