Recently noted by Derick Rethans in an informative blog post, there is (now was) a problem in the implementation of namespaces in the soon-to-be-released PHP 5.3. I quote:
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.
This morning, I realized that this would be very easy to fix, and posted a patch to internals@ with a brief explanation. Dmitry committed a fix to PHP_5_3 and HEAD very quickly. The issue raised by Derick is now obsolete, making namespaces not just more useful, but even more powerful than they were.
For instance, it is possible to "override" an internal class, perhaps to add missing functionality that will be present in a future release or fix a bug:
mydate.php:
<?php
namespace Foo;
class DateTime extends ::DateTime
{
// methods here
}
?>
<?php
include 'mydate.php';
use Foo::DateTime; // this essentially overrides internal DateTime class in the current script
$a = new DateTime('2006-05-04');
?>
Those who were lamenting the implementation of namespaces in PHP should take note: if you have a solution to make them better, the turnaround time between proposal to the list and commit can be on the order of a few hours if the idea clearly solves a major problem.