Php 异常致命错误,带有“名称已在使用中”


Php unusual fatal error with "name is already in use"

在我的本地开发机器(php 5.3.14)上,我可以使用这样的类:

<?php
namespace Shop'Repository;
use Shop'Entity'Day;
use Doctrine'ORM'EntityRepository;
class Product extends EntityRepository
{
    // Code
}

该类存储在/my/src/Shop/Repository/Product.php(符合 PSR-0 标准)中。我还有一个位于/my/src/Shop/Repository/Day.php的Shop'Repository'Day

但是,在我的登台服务器(php 5.3.10)上,我收到以下错误:

PHP 致命错误:无法使用 Shop''Entity''Day 作为 Day,因为该名称已在/my/src/Shop/Repository/Product.php第 5 行中使用

我可以理解该消息,如果我将我的商店''实体''日导入别名到日实体,代码就可以工作。但是我不明白致命错误的原因:为什么这适用于php 5.3.14(或至少适用于我的配置)而不是5.3.10(或至少适用于服务器的配置)?

我想问题是因为在命名空间Shop'Repository中已经加载了一个Day。但这从未导致我的设置错误!这是怎么回事?

以下是我对这种情况的一些解释:

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()在你的服务器和站上查看有什么不同,因为加载它们的顺序很重要

这些解释链接自德米特里评论的这篇不错的帖子

希望这能有所帮助