Re: Re: namespaces: import name conflicts with defined class

From: Date: Wed, 03 Oct 2007 14:42:04 +0000
Subject: Re: Re: namespaces: import name conflicts with defined class
References: 1 2 3 4 5  Groups: php.internals 
Request: Send a blank email to internals+get-32603@lists.php.net to get a copy of this message
<?php namespace test; class Exception {} $a = new Exception('hi'); ?> The above code works just fine. Yes there is a concept. import is best used within a namespace. So, namespace your code and there is no need to import global classes.
Yep, it works if your whole application uses one namespace, but what if you have a core package that defines some exceptions and another package, with a different namespace, that wants to use the core exceptions? Like namespace App; class Exception extends Exception { ... } and in another File: namespace App::Db; import App::Exception; .... This doesn't work. And worse: imagine PEAR would bring a PEAR::Exception along, PEAR would be my basic framework and i wouldn't care about ::Exception, i don't even know it's there - i am using PEAR. In every Script i would like to use PEAR::Exception in a convenient way i have to write: <?php import ::Exception AS WTF; import PEAR::Exception; ?> (Nevermind if this example doesn't fit the PEAR-idea, just s/PEAR/randomFramework/g then) You think this makes sense? Or what about this: pear/Mail_IMAP/IMAP.php (dunno if this file exists): <?php namespace PEAR::Mail::IMAP; import ::Exception AS WTF; import PEAR::Exception; class Imap { ... } All this seems just wrong to me, sorry. And i fully agree with Markus:
But how to know in advance, e.g. think about other libraries, which public classes possibly exist? Isn't this problematic then?
Let's say i PEAR would have namespaces and there is a class PEAR::DB::Mysql, i use this class as: <?php import PEAR::DB::Mysql; class MyDb extends Mysql { ... } And surprisingly someday mysqlnd declares a class named "Mysql", that would break my code even though i explicitly declared that i want "PEAR::DB::Mysql" not some different Mysql class that comes from some fancy extension the admin added today. So, why this behaviour? regards, Benjamin

« previous php.internals (#32603) next »