Re: a way to fix the import issue

From: Date: Thu, 04 Oct 2007 14:56:09 +0000
Subject: Re: a way to fix the import issue
References: 1 2  Groups: php.internals 
Request: Send a blank email to internals+get-32632@lists.php.net to get a copy of this message
Dmitry Stogov wrote: > In your example : > > <?php > namespace Foo; > import Blah::Exception; > $a = new Exception; > ?> > > "new Exception" refer to "Blah::Exception" and will fail if such class > doesn't exists. > > "import Blah::Exception" creates an alias with short name "Exception" only > for current file (it doesn't creates "Foo::Exception") > > May be I didn't understood the question. :) Yes, you do misunderstand I think :) testme.php: <?php namespace Blah; class Exception extends ::Exception {} ?> test.php: <?php namespace Foo; include 'testme.php'; import Blah::Exception; $a = new Exception; ?> result: cellog@lot-49:~/workspace/php5$ sapi/cli/php -n test.php Fatal error: Import name 'Exception' conflicts with defined class in /home/cellog/workspace/php5/test.php on line 4 This is because of this check: if (zend_hash_exists(CG(class_table), lcname, Z_STRLEN_P(name)+1)) { zend_error(E_COMPILE_ERROR, "Import name '%s' conflicts with defined class", Z_STRVAL_P(name)); } we are comparing "Exception" to "Exception" in the class table, and so we get a fatal error. We should instead transparently import Blah::Exception not as "Exception" but as "Foo::Exception" *only* for the above comparison check. In other words, import needs to honor namespace when checking for class naming conflicts. If you can create class "Exception" inside namespace Foo and refer to it as "Exception" then import Blah::Exception should also allow referring to "Exception" *if* "Foo::Exception" doesn't already exist. Greg

« previous php.internals (#32632) next »