不能在PHP中的另一个命名空间内使用Namespaced类

Jas*_*vis 5 php namespaces

我仍然遇到PHP5命名空间的问题.

我有一个名为的命名空间Project,我试图访问一个registry在这个Project名称空间内部调用的类,该名称空间的名称空间Library位于文件的顶部,这是一个Project命名空间我使用这一行use Library\Registry;

Registryclass在LibraryNamespace中

这应该可以工作,但它没有,相反,registry在这个Project命名空间内访问我的类的唯一方法是使用它

$this->registry = new \Library\Registry;
Run Code Online (Sandbox Code Playgroud)

我希望能够使用它而不是......

$this->registry = new Registry;
Run Code Online (Sandbox Code Playgroud)

这就是使用的全部原因

use Library\Registry;
Run Code Online (Sandbox Code Playgroud)

Project命名空间文件的顶部


下面我在这样的文件夹结构中有3个小的示例脚本.
Library/registry.class.php我的Library文件夹中的
Controller/controller.class.php类和Controller目录中
Controller/testing.php的类是运行脚本的测试文件.

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

<?php
namespace Library
{
    class Registry
    {
        function __construct()
        {
            echo 'Registry.class.php Constructor was ran';
        }
    }
}
?>
Run Code Online (Sandbox Code Playgroud)

E:\ Controller\Controller.class.php文件

<?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>';
        }
    }
}
?>
Run Code Online (Sandbox Code Playgroud)

E:\ Controller\testing.php文件

<?php
use Project\Controller;

include('testcontroller.php');

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

?>
Run Code Online (Sandbox Code Playgroud)

我收到这个错误......

Fatal error: Class 'Project\Registry' not found in PATH to file
Run Code Online (Sandbox Code Playgroud)

除非我在controller.class.php文件中使用以下内容

$this->registry = new \MyLibrary\Registry;
Run Code Online (Sandbox Code Playgroud)

因为在顶部的那个文件中,use Library\Registry;我应该能够像这样访问它...

$this->registry = new Registry;
Run Code Online (Sandbox Code Playgroud)

请帮我把它拿到哪里,我可以这样使用它

lon*_*day 6

use Library\Registry;

namespace Project
{
Run Code Online (Sandbox Code Playgroud)

相信这是南辕北辙:你是use荷兰国际集团Library\Registry在全局命名空间,然后在打开Project的命名空间.

use语句放在要将其导入的名称空间中.

namespace Project
{
    use Library\Registry;
Run Code Online (Sandbox Code Playgroud)