note 80192 deleted from language.namespaces.rules by felipe
| From: | felipe@php.net | Date: | Sat, 29 Mar 2008 15:59:50 +0000 |
| Subject: | note 80192 deleted from language.namespaces.rules by felipe | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-137718@lists.php.net to get a copy of this message | ||
Note Submitter: Dennis
----
Sadly enough, the latest class name resolution rules render PHP namespaces quite useless. It is
impossible to use class names which coincide with PHP's internal class names within your
namespaces unless you _explicitly_ use them and alias them!
The worst thing is that PHP internal class names _always_ will take precedence over your class
names. This means that you start developing a toolkit and take good care to use and alias your class
names (eg, Exception or DateTime or PDO). You also create classes with names that don't have
names that would conflict with PHP's internal classes. However, after you have done lots of
work on your toolkit you find out that there's been another extension added to PHP and it has a
class that coincides with your class name. Your code becomes useless.
A quick example:
<?php
/*
* (PDO.php)
*/
namespace my::name::space;
class PDO {}
?>
<?php
/*
* (Test.php)
*/
namespace my:name::space;
class Test {
static function doSmth() {
$pdo = new PDO();
}
}
?>
and this code:
<?php
include('PDO.php');
include('Test.php');
use my::name::space::Test;
Test::doSmth();
?>
This of course will emit a warning that PDO constructor expects at least one parameter (which is
perfectly correct for PHP Data Objects). Now go to your php.ini and disable lines
extension=php_dll.php
or
extension=pdo.so
as well as all other PDO drivers. Now run the example and it will produce no warnings!
I have used PDO class name here as it can be easily disabled to demonstrate the problem. Now imagine
that PDO was not an internal class and you were using it in your code for some time already.
Suddenly an extension author choses to use this name. And all your code is broken. You will have to
go into every file that references that class and explicitly use it with an alias and rename all
occurences of this class name to an alias.
As to me this is too far from solving the name collision issues!