我仍然遇到PHP5命名空间的问题.
我有一个名为的命名空间Project
,我试图访问一个registry
在这个Project
名称空间内部调用的类,该名称空间的名称空间Library
位于文件的顶部,这是一个Project
命名空间我使用这一行use Library\Registry;
Registry
class在Library
Namespace中
这应该可以工作,但它没有,相反,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)
请帮我把它拿到哪里,我可以这样使用它
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)
归档时间: |
|
查看次数: |
5976 次 |
最近记录: |