php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #42859 import always conflicts with internal classes
Submitted: 2007-10-05 02:03 UTC Modified: 2007-10-17 10:01 UTC
From: greg at chiaraquartet dot net Assigned: dmitry (profile)
Status: Closed Package: Scripting Engine problem
PHP Version: 5CVS-2007-10-05 (CVS) OS: linux
Private report: No CVE-ID: None
 [2007-10-05 02:03 UTC] greg at chiaraquartet dot net
Description:
------------
zend_do_import incorrectly compares an import name to global class names.  It should compare EG(current_namespace) . '::' . importname to check for conflicting names.

This is because when inside a namespace, to the user, import should behave in the same manner as a class declaration.  This code:

<?php
namespace Foo;
class Exception {}
?>

creates Foo::Exception.

This code:

<?php
namespace Foo;
import Blah::Exception as Exception;
?>

Should essentially work as if the user had typed:

<?php
namespace Foo;
import Blah::Exception as Foo::Exception;
?>

In other words, it should resolve future uses of "Exception" to "Blah::Exception" and "::Exception" to "Exception" and not complain about "Exception" conflicting with "::Exception"

Patch for PHP 5:
http://pear.php.net/~greg/fix_import.patch.txt
Patch for PHP 6:
http://pear.php.net/~greg/fix_import.php6.patch.txt

Reproduce code:
---------------
testme.php:
<?php
namespace Blah;
class Exception extends ::Exception {}
?>

test.php:
<?php
namespace Foo;
include 'testme.php';
import Blah::Exception;
$a = new Exception;
throw $a;
?>



Expected result:
----------------
Fatal error: Uncaught exception 'Blah::Exception' in /home/cellog/workspace/php5/test.php:5
Stack trace:
#0 {main}
  thrown in /home/cellog/workspace/php5/test.php on line 5


Actual result:
--------------
Fatal error: Import name 'Exception' conflicts with defined class in
/home/cellog/workspace/php5/test.php on line 4


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2007-10-05 02:16 UTC] cellog@php.net
accidentally didn't log in as cellog to submit this.
 [2007-10-05 06:56 UTC] dmitry@php.net
Patch seems to be proper. I'll review and commit it later.
 [2007-10-05 14:26 UTC] cellog@php.net
both patches were missing a zend_str_tolower call, I've updated them to include this
 [2007-10-17 09:59 UTC] dmitry@php.net
The patches are completely right, however I'll optimize them a little bit.
Thank you for catching and fixing this issue.
 [2007-10-17 10:01 UTC] dmitry@php.net
This bug has been fixed in CVS.

Snapshots of the sources are packaged every three hours; this change
will be in the next snapshot. You can grab the snapshot at
http://snaps.php.net/.
 
Thank you for the report, and for helping us make PHP better.


 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Wed Apr 24 17:01:30 2024 UTC