无法在PHP中的另一个命名空间内使用命名空间类

无法在PHP中的另一个命名空间内使用命名空间类,php,namespaces,Php,Namespaces,我仍然在PHP5名称空间方面遇到问题 我有一个名为Project的名称空间,我试图访问Project名称空间内名为registry的类,该名称空间的名称空间为Library,因此在项目名称空间的文件顶部,我使用这一行use Library\registry 注册表类位于库命名空间内 这应该可以工作,但不能,相反,访问此项目命名空间中的注册表类的唯一方法是使用 $this->registry = new \Library\Registry; 我想用这个来代替 $this->regis

我仍然在PHP5名称空间方面遇到问题

我有一个名为
Project
的名称空间,我试图访问
Project
名称空间内名为
registry
的类,该名称空间的名称空间为
Library
,因此在
项目
名称空间的文件顶部,我使用这一行
use Library\registry

注册表
类位于
命名空间内

这应该可以工作,但不能,相反,访问此
项目
命名空间中的
注册表
类的唯一方法是使用

$this->registry = new \Library\Registry;
我想用这个来代替

$this->registry = new Registry;
这就是使用的全部原因

use Library\Registry;
项目
命名空间文件的顶部


在下面的文件夹结构中,我有3个小的示例脚本。
Library/registry.class.php
my Library文件夹中的类
Controller/Controller.class.php
和我的控制器目录中的类
Controller/testing.php
运行脚本的测试文件

E:\Library\Registry.class.php文件

<?php
namespace Library
{
    class Registry
    {
        function __construct()
        {
            echo 'Registry.class.php Constructor was ran';
        }
    }
}
?>
<?php
use Library\Registry;

namespace Project
{
    class Controller
    {
        public $registry;

        function __construct()
        {
            include('E:\Library\Registry.class.php');

            // This is where my trouble is
            // to make it work currently I have to use
            //  $this->registry = new /Library/Registry;
            // But I want to be able to use it like below and that is why
            // I have the `use Library\Registry;` at the top
            $this->registry = new Registry;
        }

        function show()
        {
            $this->registry;
            echo '<br>Registry was ran inside testcontroller.php<br>';
        }
    }
}
?>
<?php
use Project\Controller;

include('testcontroller.php');

$controller = new Controller();
$controller->show();

?>
除非我在controller.class.php文件中使用下面的命令

$this->registry = new \MyLibrary\Registry;
因为在顶部的文件中,我有
use Library\Registry我应该可以像这样访问它

$this->registry = new Registry;
请帮我把它拿到我可以这样使用的地方,只需添加: 使用\库\注册表

在名称空间声明下的脚本顶部

那么你可以说:

$registry=新注册表

在你们班里

顺便说一句,你的类声明全错了。您不应该将名称空间用大括号括起来,因为名称空间不是一个函数

应该是这样的。另外,请确保已使用include('/path/to/Registry.php')包含Library\Registry的类声明;或者使用自动加载器

namespace Project;
包括('E:\Library\Registry.class.php')

use\Library\Registry;
类控制器
{
公共资源登记处;
函数_u构造()
{
//这就是我的麻烦所在
//为了使它目前的工作,我必须使用
//$this->registry=new/Library/registry;
//但我希望能够像下面那样使用它,这就是为什么
//我在顶部有“use Library\Registry;”
$this->registry=新注册表;
}
函数show()
{
$this->registry;
echo“
注册表在testcontroller.php内部运行”; } }
享受

我认为这是错误的做法:在全局名称空间中使用
use
ing
Library\Registry
,然后打开
Project
名称空间

use
语句放入要将其导入的命名空间中

namespace Project
{
    use Library\Registry;

您需要在
项目
名称空间中导入
注册表
类,因为您需要em,而不是在全局范围内

<?php   
namespace Project
{
    use Library\Registry;

    class Controller
    {
        public $registry;

        function __construct()
        {
            include('E:\Library\Registry.class.php');

            // This is where my trouble is
            // to make it work currently I have to use
            //  $this->registry = new /Library/Registry;
            // But I want to be able to use it like below and that is why
            // I have the `use Library\Registry;` at the top
            $this->registry = new Registry;
        }

        function show()
        {
            $this->registry;
            echo '<br>Registry was ran inside testcontroller.php<br>';
        }
    }
}
?>


难道你没看到我在controller.class.php文件中已经有了它吗?将名称空间括在花括号中没有什么错。很抱歉,我真的不知道用大括号包装名称空间,只是从来没有见过有人这样做。@Dmitri Snytkine也很抱歉,我想我第一次读错了你的答案,你现在提供了正确的答案。另一方面,我真的很喜欢你做的项目,Q&A项目,如果你不介意的话,也许我可以给你发一封关于名称空间以及你在项目中如何做的电子邮件?事实上,文档说你不能这样做,如果你在设置名称空间后使用
use
,那将是一个致命的错误。在我的示例中,它导致了这个
解析错误:语法错误,意外的T_类,应为“,”或“;”在第8行的E:\Controller\Controller.class.php中
@jasondavis No,这是因为我忘了在语句末尾加分号。用我编辑的版本试试看。(顺便说一句,这应该是相当明显的错误…)你是对的,我应该发现这一点。它似乎在起作用,这是应该一直这样做的吗?在名称空间声明之后?每次我认为我完全理解了这些名称空间,就会出现一些不起作用的东西,我应该说,使用多个名称空间是我的麻烦开始的地方。谢谢你的帮助。在看C#项目时,它们是以相反的方式进行的,因此我认为这增加了混淆是的,您总是需要导入到名称空间或全局范围中。
use Library\Registry;

namespace Project
{
namespace Project
{
    use Library\Registry;
<?php   
namespace Project
{
    use Library\Registry;

    class Controller
    {
        public $registry;

        function __construct()
        {
            include('E:\Library\Registry.class.php');

            // This is where my trouble is
            // to make it work currently I have to use
            //  $this->registry = new /Library/Registry;
            // But I want to be able to use it like below and that is why
            // I have the `use Library\Registry;` at the top
            $this->registry = new Registry;
        }

        function show()
        {
            $this->registry;
            echo '<br>Registry was ran inside testcontroller.php<br>';
        }
    }
}
?>
<?php namespace Project;
    require_once 'your registry class file';
    use \Library\Registry as Registry;
    $this->registry = new Registry;