我仍然遇到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;
请帮我把它拿到哪里,我可以像那样使用它
答案 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;