Set default namespace
| From: | Tomi Kaistila | Date: | Thu, 10 Jan 2008 00:05:50 +0000 |
| Subject: | Set default namespace | ||
| Groups: | php.internals | ||
| Request: | Send a blank email to internals+get-34624@lists.php.net to get a copy of this message | ||
Sorry for the ambiguous subject line, but I could not find a better wording
for it.
Currently, when you use namespaces in PHP, whenever you want to call a
function from inside a namespace you must either use the entire name of the
namespace (potentially very long) or give it a shorter alias.
Here is an example:
namespace.inc
namespace ProjectA;
const WORD = 'foobar';
execute.php
use ProjectA as A;
print A::WORD;
This works out fine. But when I change the two last lines in execute.php to:
use ProjectA;
print WORD;
I get a warning saying "The use statement with non-compound name 'ProjectA'
has no effect ..." and naturally also a notice about using a non-defined
constant.
So, the default namespace in use, is always global. What about if the default
could be changed? It would put to rest the need to always specify the
namespace from which to look for the function or class. This would mean that
the last example of execute.php would be valid. You could also still access
the global space with the following syntax:
execute.php
const BOOK = 'Two Towers';
use ProjectA;
print ::BOOK;
Any thoughts on this? Was this sort of approach considered when the namespaces
were being implemented?
Tomi Kaistila
PHP Developer