Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot use X as Y because the name is already in use, even though it's not

Tags:

php

I'm using PHP 5.4, and have a PSR-0 class structure similar to the following.

A\Library\Session.php:

namespace A\Library;

class Session { ... }

My\Application\Session.php:

namespace My\Application;

class Session { ... }

My\Application\Facebook.php:

namespace My\Application;
use A\Library\Session;

class Facebook { ... }

When I try to run the application, I get the following error:

Cannot use A\Library\Session as Session because the name is already in use in My\Application\Facebook.php

Even though it's not, at least not in this file. The Facebook.php file declares only the Facebook class, and imports exactly one Session class, the A\Library one.

The only problem I can see is that another Session class exists in the same namespace as the Facebook class, but as it was never imported in the Facebook.php file, I thought it did not matter at all.

Am I wrong (in that case please point to the relevant documentation), or is this a bug?

like image 746
BenMorel Avatar asked Jul 19 '13 12:07

BenMorel


2 Answers

There is a bug confirmed in PHP that may affect the behavior you see. It is supposed to fatal error, but with opcache enabled, it may still execute flawlessly.

https://bugs.php.net/bug.php?id=66773

If it still concerns you, please vote for the bug.

like image 136
PowerKiKi Avatar answered Oct 17 '22 23:10

PowerKiKi


No, this is not a bug. As mentioned in Using namespaces: Aliasing/Importing

use A\Library\Session;

is the same as:

use A\Library\Session as Session;

So try using something like:

use A\Library\Session as AnotherSessionClassName;
like image 28
Mohsenme Avatar answered Oct 17 '22 23:10

Mohsenme