Blog Posts

PHP is (most presumably) getting namespaces

If you're not subscribed on the internals mailinglist of PHP, you may have missed a (most of the time very ridiculous) discussion about namespaces in PHP. The important part of the whole discussion is, that it looks like PHP 6 is getting namespaces. I don't care that much about namespaces, I prefix my classes anyway, but it's a nice to have certainly.

The annoying part of the discussion (with dozens of redundant posts) was about which namespace separator should be used. The proposals went from ~> to }{ to :> and other funny combinations and someone even started counting the votes and making a big table of his interpretation what the people said... You have to keep in mind, that there are usually no votes on internals, but conclusions are tried to be made by consensus.

Anyway, at the end it boils down to either :: or ::: or as separator, the double colons have some technical limitations, the backslash looks somehow strange, but I think, one could get used to it. How this would look like (nested namespaces are possible):

name1:::name2:::class::func();
name1\name2\class::func();

The ::: is really hard to parse IMHO, so I'd prefer , even though it's usually the escaping character. But I'm not the one to decide in that matter (neither are most of the people who have flooded the mailing list in the last few days...)

About the author

Comments [9]

Patrice, 29.11.2005 10:58 CEST

Double colon seems fine to me, reminds me of C++ namespaces.
(http://www.cplusplus.com/doc/tutorial/namespaces.html)

chregu, 29.11.2005 11:03 CEST

Double-colons have some technical limitations (especially if namespaced functions should be allowed), 'cause they are already used for static methods.

Details on that can be found on the mailinglist (between all the noise :) ), as I'm not familiar right now with the real limitations.

Nico Edtinger, 29.11.2005 13:36 CEST

If you entered the backslash in your examples it's a nice example why it's a bad idea. It means escaping in nearly every other context (exception windows paths *ugh*). And it seems like it got dropped by your self written blog =)

b4n

chregu, 29.11.2005 14:46 CEST

Hrhr, grrreat. You won :)

Although it's just a bug somewhere in my Blog Tool...

Hermit, 29.11.2005 17:59 CEST

IMO, they should kill off the hash mark as a commenting character because everyone knows better than to use # comments (we already have // and /* */, what more is needed?). And then use the hash for namespaces:

Name1#Name2#Class::StaticMethod();

Ivo Jansch, 29.11.2005 19:52 CEST

A dot would be nice, but ofcourse, this is used for string concatenation. Now that could easily be replaced by +, but that would raise serious backwardscompatibility issues ofcourse.

timvw, 29.11.2005 19:56 CEST

The problem is that # is already in use...

#!/usr/bin/env php
<?php
...
?>

Christopher Hayes, 30.11.2005 00:57 CEST

Double colons don't necessarily have a technical limitation, and name spaces already exist, in some fashion, in PHP.

All a class is is a name-space with added functionality. This is from the origins of C & C++, where name spaces were extended to allow functions, then objectification.

I've even chided my boss for calling his programming object-oriented, when all he'd really done is create a few name-space classes (just a class that collects a bunch of functions, and sometimes a variable or two in his case), then *objectify* it for some ungodly reason, when everywhere he uses it it could be used statically - as a namespace.

Long-winded, I know... but basically, my argument is that name-spaces & classes use the same symbols in C++, they can & should in this case too... since classes really are just extended namespaces.

Super Mike, 01.12.2006 19:55 CEST

Forgive me for being new to the guts of PHP -- I've mostly just been an avid fan of using the language.

I've wanted namespaces for awhile, but I ask why don't we do the following?

1. Require string concatenation be " . " with a space before and after.

2. Use the "." alone (with no space) to be the namespace separator.

3. Permit one to use "." instead of "->" if they want to start doing that.

Perhaps item 3 might not be possible with the parser, but I would think that 1 and 2 would be.

If we did this, it would be more congruent and far easier to type with other curly bracket type languages.

As is, using ":::" seems a bit too many keystrokes. I prefer Bluefish as my IDE and it doesn't have intellisense to automatically put that in for me. It adds to my typing.

I'll work with whatever I'm given, but my vote is for the simple "."

Add a comment

Your email adress will never be published. Comment spam will be deleted!