[英]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.