note 80192 added to language.namespaces.rules
| From: | Dennis at osu1 dot php dot net | Date: | Thu, 03 Jan 2008 19:18:14 +0000 |
| Subject: | note 80192 added to language.namespaces.rules | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-134726@lists.php.net to get a copy of this message | ||
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!
----
Server IP: 216.92.131.65
Probable Submitter: 194.44.193.103
----
Manual Page -- http://www.php.net/manual/en/language.namespaces.rules.php
Edit -- https://master.php.net/note/edit/80192
Del: integrated -- https://master.php.net/note/delete/80192/integrated
Del: useless -- https://master.php.net/note/delete/80192/useless
Del: bad code -- https://master.php.net/note/delete/80192/bad+code
Del: spam -- https://master.php.net/note/delete/80192/spam
Del: non-english -- https://master.php.net/note/delete/80192/non-english
Del: in docs -- https://master.php.net/note/delete/80192/in+docs
Del: other reasons-- https://master.php.net/note/delete/80192
Reject -- https://master.php.net/note/reject/80192
Search -- https://master.php.net/manage/user-notes.php