php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #70135 class alias conflict with class defined in another file with the same namespace
Submitted: 2015-07-25 16:08 UTC Modified: 2015-08-02 21:46 UTC
From: php at lvht dot net Assigned:
Status: Not a bug Package: Scripting Engine problem
PHP Version: 5.6.11 OS: linux & mac os x
Private report: No CVE-ID: None
View Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
If you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: php at lvht dot net
New email:
PHP Version: OS:

 

 [2015-07-25 16:08 UTC] php at lvht dot net
Description:
------------
In one file with a namespace(eg, Lv), we define a class named Foo.

In another with a subnamespace(eg, Lv\Common), we define another class with the *same* name Foo. 

In the third file with the same namespace(Lv), we first "import" Lv\Common\Foo using "use Lv\Common\Foo" and then define a class named Bar that extends Foo(the Lv\Common\Foo).

In a test file, we first make an instance of Lv\Foo, and then try to make an instance of Lv\Bar, which will failed with a fatal error.

Test script:
---------------
We need four files in the same directory.

1. In Foo.php,

<?php namespace Lv;
class Foo{}

2. In CommonFoo.php,

<?php namespace Lv\Common;
class Foo{}

3. In Bar.php,

<?php namespace Lv;
require 'CommonFoo.php';
use Lv\Common\Foo;
class Bar extends Foo {}

4. Finally, in app.php,
<?php
require 'Foo.php';
$foo = new Lv\Foo;
require 'Bar.php';
$bar = new Lv\Bar; # error will trigged by this line

Just run php app.php, you will see a fatal error.


Expected result:
----------------
The app.php should run without any error.

Actual result:
--------------
The error given,

PHP Fatal error:  Cannot use Lv\Common\Foo as Foo because the name is already in use in ...Bar.php  on line 3

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2015-07-27 13:59 UTC] laruence@php.net
see also : http://3v4l.org/Ootud
 [2015-07-27 14:12 UTC] laruence@php.net
actually, this is quite confused one.

since you are trying to use \Lv\Common\Foo as \Lv(current_namespace)\Foo

but you already have one \Lv\Foo defined in Foo.php,

so when you try to refer "Foo" in namespace Lv(Bar.php), which Foo should be used?

thanks
 [2015-07-27 15:25 UTC] php at lvht dot net
If we first make an instance of Lv\Bar, and then Lv\Foo, the script runs happily. Surprise!!!

see also: http://3v4l.org/5NF4Y

I think the alias name generated by the use statement should only take effect in the source file canting the statement.

In our case, we put 'use Lv\Common\Foo' in the Bar.php, and then we define a Bar class extends the Foo *Alias*. In my opinion, the alias of use statement in on script file should only not to conflict with the symbol defined in the same file. 

If an alias defined in specific file will conflict with class in other file, the programmer should take extra burden to avoid the collision when import symbol using use statement.

In my opinion, what we want the use statement to do is not defined the alias as a symbol the current namespace, but just as an "Shortcut" of the original class. When PHP execute this code, the engine should be replace the "shortcut" by the original class, like C's macro.

Thanks.
 [2015-08-02 21:46 UTC] bwoebi@php.net
-Status: Open +Status: Not a bug
 [2015-08-02 21:46 UTC] bwoebi@php.net
Not sure why that's a surprise?
An use statement is always local to the current namespace block.

Basically in your bug report, it's:
Register class.
Register "use" alias. # conflict with class

In your 3v4l script now, it's:
Register "use" alias
Leave namespace-block (the eval is delimiting the namespace here): unregister the alias
Register class. # no conflict, "use" alias already unregistered.

See: http://php.net/manual/en/language.namespaces.importing.php
Docs compare it to a [temporary] symbolic link. If you create a link, remove it and then put a file there, no problem. But if you put a file there and try to add a symbolic link with the same name, you'll obviously fail.

Thus I'm closing that as documented and expected behavior / not a bug.
 
PHP Copyright © 2001-2026 The PHP Group
All rights reserved.
Last updated: Tue Oct 06 03:00:02 2026 UTC