13

在我的本地开发机器(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)中。我有一个Shop\Repository\Day位于/my/src/Shop/Repository/Day.php。

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

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

我可以理解该消息,如果我将 Shop\Entity\Day 导入别名为 DayEntity,则代码有效。但我无法理解致命错误的原因:为什么这适用于 php 5.3.14(或至少,使用我的配置)而不是 5.3.10(或至少,使用服务器的配置)?

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

4

1 回答 1

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

这些解释链接自dmitry评论的这篇不错的帖子

希望这可以帮助

于 2012-08-16T14:29:46.217 回答