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

时间:2011-12-23 14:38:51

标签: php namespaces

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

我有一个名为Project的命名空间,我正在尝试访问名为registry的名为Project的名称空间,其名称空间为Library,因此在顶部作为Project命名空间的文件我使用此行use Library\Registry;

Registry类位于Library命名空间

这应该有效,但它不起作用,而是在registry命名空间内访问我的Project类的唯一方法是使用此

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

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

$this->registry = new Registry;

这就是使用

的全部原因
use Library\Registry;

位于Project命名空间文件的顶部


下面我在这样的文件夹结构中有3个小例子脚本。
Library/registry.class.php我的图书馆文件夹中的一个班级 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';
        }
    }
}
?>

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文件

<?php
use Project\Controller;

include('testcontroller.php');

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

?>

我收到此错误...

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

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

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

因为在顶部的文件中我有use Library\Registry;我应该可以像这样访问它...

$this->registry = new Registry;

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

4 个答案:

答案 0 :(得分:5)

use Library\Registry;

namespace Project
{

相信这是错误的方式:你在全局命名空间中use Library\Registry,然后打开Project命名空间。

use语句放在要导入的名称空间中。

namespace Project
{
    use Library\Registry;

答案 1 :(得分:2)

您需要在Registry命名空间内导入Project类,因为您需要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>';
        }
    }
}
?>

答案 2 :(得分:1)

添加: 使用\ Library \ Registry;

在命名空间声明

下的脚本顶部

然后你可以说:

$ registry = new Registry;

你班上的

顺便提一下你的班级宣言是错的。你不应该将你的命名空间包装在花括号中,命名空间不是一个函数。

这应该是这样的。还要确保使用include('/ path / to / registry.php')包含Library \ Registry的类声明;或使用自动加载器

namespace Project;

包括( '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>';
        }
    }

享受

答案 3 :(得分:0)

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

现在你可以使用......

    $this->registry = new Registry;