Namespaces in PHP
During Stefan Priebsch' session at the Dutch PHP Conference on PHP 5.3 and PHP 6 - A look ahead a discussion popped up about PHP's namespace support. One of the things that came up is the conflicts that can arise with internal classes.
Take for example this code:
<?php use PEAR::Date::Interval as Interval; ?>
In PHP 5.3 this would alias the class Interval in the namespace PEAR::Date to the class Interval. For now, this code would work just fine. However, if PHP would introduce a class "Interval" at some point in the future (and PHP can do this as it owns the global namespace ) then the above code would suddenly stop working. So in order to use namespaces properly, you always need to have at least two elements left, like:
<?php use PEAR::Date as pd; $interval = new pd::Interval; ?>
You need to make sure of course that the short name that you pick is something that does not sound to much like a class name otherwise you'll have exactly the same issue. It's not very likely that PHP will introduce a class pd though.
Comments
I think we should extend the userland naming guide for 5.3 with a warning on this point.
and obscure naming conventions suddenly return.
sigh
Hi,
It is important to note that with the current namespace design, this issue only occurs if your code is non-namespaced. The following code will continue to work ad infinitum:
The only case to worry about would be a user class named Interval in the namespace Date, but again this would be a problem no matter what, which is why when you choose a namespace prefix, it is wise to choose one that is not a common name (like PEAR2, for instance)
This is why I suggest that all code using namespaced code should pop a "namespace __user__;" at the top, and PHP reserve the __user__ namespace for top-level code.
Of course, there is a still some time to closely review the namespace implementation, as the gotchas are fierce if you don't properly import namespaced classnames with the "use" keyword. I highly suggest all smart people apply themselves to some of the harder problems raised, such as the order of resolution of unqualified names like "Interval" (::Date::Interval is a fully qualified name because it starts with ::)
Thanks for this Derick. Informative.
-
C
I have raised this problem about 6 months ago in comments to this manual page http://php.net/manual/en/language.namespaces.rules.php
However, my comment was deleted! You can see that the only comment on that is referencing the deleted one.
And, finally, regarding this "workaround". It is not 100% safe, and as well as the inability of PHP5.3 to do "use some::namespace::*" will often result in more typing (compared to pear-like naming), as several bloggers have pointed out in the past.
Dennis, that's because such things should not end up in comments in the manual. You should instead have brought it up on the php-internals mailing list.
This problem also exists in other C style programming languages. A potential solution, its to use a separate operator for classes and for namespaces, e.g. ¨MyNamespace!!Anything¨ and ¨MyClass::Anything¨
In reply to mramirez: This is not a problem of "C style programming languages". The problem is that the C programmers know only the C++ namespace implementation. :)
Life Line
Updated an information and a bench
Created 2 benches
Created 4 picnic_tables, a bench, and a fitness_station; Updated a pub and a sport club
Created 2 benches and 2 waste_baskets; Updated 4 benches, a bus_stop, and a cafe; Confirmed a dentist
Created 3 waste_baskets, 2 main entrances, and a bench; Deleted a cycle_barrier, a bench, and a waste_basket
Created an information; Updated a waste_basket and a bench
Updated 2 waste_baskets and a bench
Created 3 benches
Created a waste_basket; Updated 2 benches and a tree
I walked 3.5km in 35m31s
Created a main entrance and a home entrance
Created an entrance
Updated a house building
Created an entrance
I walked 5.8km in 1h15m06s
I've just finished reading "A Cheese-Monger's Tour de France", by Ned Palmer.
Now I want to try many of those! 🧀
I'm thrilled to announce that I'll be speaking at the 23rd edition of #phpday, the international PHP conference in Italy, organised by @grusp.
I’ll be presenting a talk titled: "Better Debugging With Xdebug".
It's in Verona, Italy, on May 14-15th 2026.
You can use my speaker’s discount code "speaker_10OFF" for 10% off at https://www.phpday.it/tickets/?utm_medium=organic&utm_source=linkedin&utm_campaign=post-speaker
I walked 5.5km in 1h11m00s
I walked 1.1km in 9m37s
Merged pull request #1066
PHP 8.6: Changes to opcache optimisations wrt function arguments
I walked 10.5km in 1h49m54s
Fixed building type
Fixed addresses and building type
Updated a bus_stop, a waste_basket, and a bench


Shortlink
This article has a short URL available: https://drck.me/ni-php-6d3