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


Cannot use a Namespaced Class inside another namespace in PHP

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

我有一个名为 Project 的命名空间,我正在尝试访问这个 Project 命名空间中的一个名为 registry 的类,该命名空间为 Library,因此在文件的顶部,这是一个Project命名空间,我使用此行use Library'Registry;

Registry类位于 Library 命名空间内

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

$this->registry = new 'Library'Registry;

我希望能够使用它代替...

$this->registry = new Registry;

这就是使用的全部原因

use Library'Registry;

Project命名空间文件的顶部


下面我在这样的文件夹结构中有 3 个小示例脚本。
在我的"资源库"文件夹中
Library/registry.class.php类 控制器目录中
Controller/controller.class.php和类 Controller/testing.php测试文件以运行脚本。

E:''库''注册表.class.php文件

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

E:''控制器''控制器.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>';
        }
    }
}
?>

E:''控制器''测试.php文件

<?php
use Project'Controller;
include('testcontroller.php');
$controller = new Controller();
$controller->show();
?>

我收到此错误...

Fatal error: Class 'Project'Registry' not found in PATH to file

除非我在控制器.class.php文件下面使用它

$this->registry = new 'MyLibrary'Registry;

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

$this->registry = new Registry;

请帮我把它放在我可以这样使用它的地方

use Library'Registry;
namespace Project
{

我认为这是错误的方式:您use全局命名空间中的Library'Registry,然后打开Project命名空间。

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

namespace Project
{
    use Library'Registry;
您需要将

Registry类导入命名空间Project因为您需要在那里,而不是在全局范围内。

<?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>';
        }
    }
}
?>

只需添加:使用 ''库''注册表;

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

然后你可以说:

$registry = 新注册表;

课堂内部

顺便说一下,你的类声明都是错误的。您不应该将命名空间包装在大括号内,命名空间不是一个函数。

这是应该的。还要确保通过使用 include('/path/to/registry.php' 包含库''注册表的类声明;或使用自动加载机

namespace Project;

include('E:''Library''Registry.class.php'(;

use 'Library'Registry;
    class Controller
    {
        public $registry;
        function __construct()
        {

            // 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;