php 不能将 X 用作 Y,因为该名称已在使用中,即使它不是
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17746481/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Cannot use X as Y because the name is already in use, even though it's not
提问by Benjamin
I'm using PHP 5.4, and have a PSR-0 class structure similar to the following.
我使用的是 PHP 5.4,并且有一个类似于以下的 PSR-0 类结构。
A\Library\Session.php:
A\Library\Session.php:
namespace A\Library;
class Session { ... }
My\Application\Session.php:
我的\应用程序\会话.php:
namespace My\Application;
class Session { ... }
My\Application\Facebook.php:
我的\应用程序\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
不能使用 A\Library\Session 作为会话,因为该名称已在 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.
即使不是,至少不在这个文件中。Facebook.php 文件只声明了这个Facebook
类,并且只导入了一个Session
类A\Library
。
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.
我能看到的唯一问题是另一个Session
类存在于与该类相同的命名空间中Facebook
,但由于它从未被导入到 Facebook.php 文件中,我认为这根本无关紧要。
Am I wrong (in that case please point to the relevant documentation), or is this a bug?
我错了(在那种情况下请指向相关文档),还是这是一个错误?
采纳答案by PowerKiKi
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.
在 PHP 中确认了一个错误,它可能会影响您看到的行为。它应该是致命错误,但是启用 opcache 后,它仍然可以完美地执行。
https://bugs.php.net/bug.php?id=66773
https://bugs.php.net/bug.php?id=66773
If it still concerns you, please vote for the bug.
如果它仍然与您有关,请投票支持该错误。
回答by Mohsenme
No, this is not a bug. As mentioned in Using namespaces: Aliasing/Importing
不,这不是错误。如使用命名空间:别名/导入中所述
use A\Library\Session;
使用 A\Library\Session;
is the same as:
是相同的:
use A\Library\Session as Session;
So try using something like:
所以尝试使用类似的东西:
use A\Library\Session as AnotherSessionClassName;
回答by John Cartwright
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.
我能看到的唯一问题是另一个 Session 类存在于与 Facebook 类相同的命名空间中,但由于它从未被导入到 Facebook.php 文件中,我认为这根本无关紧要。
Yes, it does matter. This is why you don't need to "import" classes from the same namespace. If you have conflicting names from different namespaces, you need to alias the class.
是的,这很重要。这就是为什么您不需要从同一命名空间“导入”类的原因。如果来自不同命名空间的名称存在冲突,则需要为类添加别名。
namespace My\Application;
use A\Library\Session as ASession; // choose a proper alias name here
class Facebook { ... }
回答by pmiranda
I've read the thread about the issue, but I tested on many PHP versions (php 5.5, 5.6, 7.*, x32, x64, vc11, vc14, vc5). I'm using Laravel with Laragon. But, when I build up the server with php artisan serve
(and open the server at http://localhost:8000) I have the problem of "the namespace that some Class was already used" and stuff.
我已阅读有关该问题的线程,但我在许多 PHP 版本(php 5.5、5.6、7.*、x32、x64、vc11、vc14、vc5)上进行了测试。我正在将 Laravel 与 Laragon 一起使用。但是,当我使用php artisan serve
(并在http://localhost:8000 上打开服务器)构建服务器时,我遇到了“某个类已经使用的命名空间”之类的问题。
I tested with and without opcache extension and nothing works, then I tested the virtual domain that Laragon provides and... voila, the error just disappeared and now I can work OK. I don't know what was happening, my namespaces were OK, I had an alias but the same code works in many machine without problem (AWS, local, prod, dev, etc) but only in my machine I had the problem just as I described it.
我在有和没有 opcache 扩展的情况下进行了测试,但没有任何效果,然后我测试了 Laragon 提供的虚拟域,......瞧,错误消失了,现在我可以正常工作了。我不知道发生了什么,我的命名空间没问题,我有一个别名,但相同的代码在许多机器上都没有问题(AWS、本地、生产、开发等)但只有在我的机器上我才遇到问题我描述了它。
So, if someone is working with Laravel (5.1) and is having this issue, try the virtual host of Laragon.
因此,如果有人正在使用 Laravel (5.1) 并且遇到此问题,请尝试使用 Laragon 的虚拟主机。