PHP 6 Namespaces
Speaking of the future of PHP, the PHP internals team has been talking about implementing namespaces in PHP 6. The problem they want to solve is this: how do you keep two class names (such as “Connection”) from interfering with each other? On a small web site, it’s easy, but once a site becomes complex or you start adding in third-party plug-ins and re-using old code, it gets much harder. Currently developers are using very long class names to help ensure their uniqueness, and no one likes that solution much.
The leading proposal at the moment was put forward by Dmitry Stogov on the PHP-Dev list. His idea is to declare the namespace at the beginning of the file and then all function names would be prefixed with the namespace name.
I like the idea, I like the “::” seperators, but I don’t like being restricted to declaring it at the top of the file. Why not just envelope the namespace anywhere (using curly-braces)? Then you don’t have to be at the top of the file and you can put more than one namespace in a file. It seems much more flexible than a single declaration in a required position.
1 2 3 4 5 6 7 | namespace Zend::DB { class Connection {...} function connect() {...} } namespace AltZend::DB { class Connection {...} } |
You could even go further and allow namespaces to be inside other name spaces.
1 2 3 4 5 6 7 8 9 10 11 12 | namespace PP { namespace DB { class Connecton {...} function connect() {...} } class Foo {...} function bar() {...} } $x = new PP::DB::Connection; PP::DB::connect(); $y = new PP::Foo; PP::bar(); |
Ruby gives this namespace behavior using Modules.
What do you think? They are looking or feedback and ideas.

August 18th, 2007 at 3:56 pm
Why not another possibility?
For example something like :
class AA::BB::CC {
}
class AA::BB::DD {
}
class AA::EE::CC {
}
$c1 = new AA::BB::CC();
$c2 = new AA::EE::CC();
Pretty simple, no ?
It could work even for non-OOP:
function AA::BB::func($param) {
}
const ZZ::YY::C = “foo”;
AA::BB::func(ZZ::YY::C);
August 20th, 2007 at 11:50 am
Definitely another candidate to consider. I’m in support of any syntax that includes curly-braces to limit the start and end of the namespace.
The one downside to your idea is that it requires rewriting “class” and I’m sure that’s a bigger project than they were hoping to take on to get namespaces implemented.
Right now, Dmitry Stogov’s patch is still the leading one, but it’s not without controversy.
August 20th, 2007 at 12:17 pm
[...] in July, I wrote that the PHP internals team was discussing implementing namespaces in PHP [...]
October 3rd, 2009 at 11:39 pm
what about “dynamic” namespaces, where the namespace identifier is defined outside of the file?
————- foo.php —————-
function foo() {}
————- bar.php —————-
function bar() {}
————- index.php ————–
// static namespace syntax
MyNamespace::include(’foo.php’);
MyNamespace::include(’bar.php’);
MyNamespace::foo();
MyNamespace::bar();
// dynamic namespace syntax
$my_namespace->include(’foo.php’);
$my_namespace->include(’bar.php’);
$my_namespace->foo();
$my_namespace->bar();
// alternative syntax
include ‘foo.php’ in $my_namespace;
// directory includes
recursive_include($wordpress_namespace, ‘lib/wordpress’);
recursive_include($mediawiki_namespace, ‘lib/mediawiki’);
————————————–
The syntax has some problems, but the idea is clear:
Responsibility for nameclashes is moved to the user code. Existing code can be namespaced from outside.
Why am I writing this here?
Because I did not find another place..