简体   繁体   English

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

[英]Cannot use a Namespaced Class inside another namespace in PHP

I am still having trouble with PHP5 Namespaces. 我仍然遇到PHP5命名空间的问题。

I have a namespace called Project and I am trying to access a class called registry inside of this Project namespace that has a namespace of Library so at the top of the file that is a Project namespace I use this line use Library\\Registry; 我有一个名为Project的命名空间,我试图在这个Project命名空间内部访问一个名为registry的类,该命名空间具有Library的命名空间,所以在文件的顶部是Project命名空间我使用这行use Library\\Registry;

Registry class is inside the Library Namespace Registry类位于Library Namespace中

This should work but it doesn't, instead the ONLY way to access my registry class inside this Project namespace is to use this 这应该工作,但它不起作用,而是在这个Project命名空间内访问我的registry类的唯一方法是使用它

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

I want to be able to use this instead... 我希望能够使用它而不是......

$this->registry = new Registry;

That was the whole reason of using 这就是使用的全部原因

use Library\Registry;

at the top of the Project namespace file Project命名空间文件的顶部


Below I have 3 small example scripts in a folder structure like this. 下面我在这样的文件夹结构中有3个小的示例脚本。
Library/registry.class.php a class in my Library folder Library/registry.class.php我的Library文件夹中的一个类
Controller/controller.class.php and class in my Controller directory Controller/controller.class.php和我的Controller目录中的类
Controller/testing.php a test file to run the script. Controller/testing.php运行脚本的测试文件。

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

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

E:\\Controller\\Controller.class.php file 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>';
        }
    }
}
?>

E:\\Controller\\testing.php file E:\\ Controller \\ testing.php文件

<?php
use Project\Controller;

include('testcontroller.php');

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

?>

I get this error... 我收到这个错误......

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

unless I use this below on the controller.class.php file 除非我在controller.class.php文件中使用以下内容

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

Because in that file at the top I have use Library\\Registry; 因为在顶部的那个文件中我use Library\\Registry; I should be able to access it like this... 我应该能够像这样访问它...

$this->registry = new Registry;

Please help me get it where I can use it like that instead 请帮我把它拿到哪里,我可以这样使用它

use Library\Registry;

namespace Project
{

I believe that's the wrong way round: you're use ing Library\\Registry in the global namespace, and then opening the Project namespace. 认为这是错误的方式:你在全局命名空间中use Library\\Registry ,然后打开Project命名空间。

Put the use statement inside the namespace you want to import it into. use语句放在要导入它的命名空间中。

namespace Project
{
    use Library\Registry;

You need to import your Registry class inside Project namespace, because you need em there, not in global scope. 您需要在Project命名空间内导入您的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>';
        }
    }
}
?>

Just add: use \\Library\\Registry; 只需添加:use \\ Library \\ Registry;

at top of your script under the namespace declaration 在命名空间声明下的脚本顶部

Then you can just say: 然后你可以说:

$registry = new Registry; $ registry = new Registry;

inside your class 在你的班级里面

By the way your class declaration is all wrong. 顺便说一句,你的班级宣言都是错的。 You should not wrap your namespace inside curly braces, namespace is not a function. 您不应将命名空间包装在花括号中,命名空间不是函数。

This is how it should be. 这应该是这样的。 Also make sure the class declaration of Library\\Registry is already included by either using include('/path/to/registry.php'); 还要确保使用include('/ path / to / registry.php')包含Library \\ Registry的类声明; or using autoloader 或使用自动加载器

namespace Project;

include('E:\\Library\\Registry.class.php'); 包括( 'E:\\图书馆\\ 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>';
        }
    }

Enjoy 请享用

<?php namespace Project;
    require_once 'your registry class file';
    use \Library\Registry as Registry;

Now you will be able to use... 现在你可以使用......

    $this->registry = new Registry;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在PHP 5.3+中的命名空间类中使用全局命名空间类型提示? - How do I use global namespace type hinting inside of a namespaced class in PHP 5.3+? PHP在另一个名称空间的类中的方法内部使用名称空间类 - PHP use namespace class inside method in a class of another namespace PHP:默认在子命名空间类中使用父命名空间 - PHP: Use parent namespace by default in child namespaced class 我必须在Laravel中使用什么命名空间或外观才能访问命名空间类中的模型? - What namespace or facade do I have to use in Laravel to have access to my models inside a namespaced class? 如何访问与另一个非命名空间类不同的命名空间中的类 - How to access a class in a different namespace from another non namespaced class 无法在PHP中导入/使用命名空间函数 - Cannot import/use a namespaced function in PHP PHP尝试使用命名空间函数(非类) - PHP Trying to Use a Namespaced Function (Not Class) 如何在具有相同名称空间的另一个类中使用一个类? - How can I use one class inside another with the same namespace? 试图从命名空间加载类。 您是否忘记了另一个命名空间的“use”语句? 使用供应商 php 命名空间 - Attempted to load class from namespace. Did you forget a "use" statement for another namespace? with vendor php namespace 将没有命名空间的类导入到命名空间类 - Importing class without namespace to namespaced class
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM