2012-08-16 42 views
13

在我的本地开发机(PHP 5.3.14),我可以使用一类这样的:腓不寻常的致命错误有“名称已在使用”

<?php 

namespace Shop\Repository; 

use Shop\Entity\Day; 
use Doctrine\ORM\EntityRepository; 

class Product extends EntityRepository 
{ 
    // Code 
} 

类存储在/我的/ src目录/Shop/Repository/Product.php(符合PSR-0)。我有 a Shop\Repository\Day位于/my/src/Shop/Repository/Day.php。

然而,我的临时服务器(PHP 5.3.10)我得到以下错误的:

PHP Fatal error: Cannot use Shop\Entity\Day as Day because the name is already in use in /my/src/Shop/Repository/Product.php on line 5

我能理解消息,如果我别名我店\实体\日进口DayEntity,代码作品。但是我无法理解致命错误的原因:为什么在PHP 5.3.14(或者至少使用我的配置)而不是5.3.10(或者至少使用服务器的配置)上工作?

我想问题是因为在命名空间Shop\Repository已经有一个Day加载。但是这从来不会导致我的设置出错!这是怎么回事?

+0

'使用Shop \ Entity \ Day作为EntityDay;',或者只是用它的完全限定名称来引用它,并完全摆脱'use'声明。也许重新阅读[this](http://www.php.net/manual/en/language.namespaces.importing.php)。 – DaveRandom 2012-08-16 14:08:17

+0

很明显,如果您将名为Day的类导入名为Day的类名称空间中,您将会遇到冲突。至于它为什么适用于您的本地设置;在与5.4.10和5.3.14之间的命名空间相关的更新日志中,我看不到任何错误修正。您在本地加载Entity \ Day时是否有可能使用您的Repository \ Day类? – Leigh 2012-08-16 14:12:07

+0

@Leigh我正在请求完全相同的页面,与git完全相同的克隆。这必须是(php)配置问题。 – 2012-08-16 14:19:11

回答

7

这里有一些解释,我抓住了这个情况:

require_once 'ns_class2.php'; 
// 
namespace ns; // Declaration of the namespace named "ns" 
class class2 {} // Declaration of the class "ns/class2" 
// In the namespace "ns", "class2" is an alias of "ns\class2" 
// 


require_once 'ns_ns1_ns2_class2.php'; 
// 
namespace ns\ns1\ns2; // Declaration of the namespace named "ns\ns1\ns2" 
class class2 {} // Declaration of the class "ns\ns1\ns2\class2" 
// In the namespace "ns\ns1\ns2", "class2" is an alias of "ns\ns1\ns2\class2" 
// 

require_once 'ns_ns1_ns2_class1.php'; 
// 
namespace ns\ns1\ns2; // Declaration of the namespace named "ns\ns1\ns2" 
// In the namespace "ns\ns1\ns2", "class2" is an alias of "ns\ns1\ns2\class2" 
use ns\class2; // Creation of the alias "class2" which point to "ns\class2" but class2 is already an alias of ns\ns1\ns2\class2 => ERROR 

所以,你应该用get_included_files尝试()来查看你的服务器和工作站哪些型动物上,因为为了加载它们是非常重要的

这些解释从this nice post这是由dmitry

希望评论链接,这可能有助于

相关问题