我仍然遇到PHP5命名空间的问题。
我有一个名为 Project
的命名空间,我正在尝试访问这个 Project
命名空间中的一个名为 registry
的类,该命名空间为 Library
,因此在文件的顶部,这是一个Project
命名空间,我使用此行use Library'Registry;
Registry
类位于 Library
命名空间内
这应该有效,但它没有,相反,在这个 Project
命名空间中访问我的 registry
类的唯一方法是使用它
$this->registry = new 'Library'Registry;
我希望能够使用它代替...
$this->registry = new Registry;
这就是使用的全部原因
use Library'Registry;
在Project
命名空间文件的顶部
下面我在这样的文件夹结构中有 3 个小示例脚本。
在我的"资源库"文件夹中Library/registry.class.php
类 控制器目录中
的Controller/controller.class.php
和类 Controller/testing.php
测试文件以运行脚本。
E:''库''注册表.class.php文件
<?php
namespace Library
{
class Registry
{
function __construct()
{
echo 'Registry.class.php Constructor was ran';
}
}
}
?>
E:''控制器''控制器.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:''控制器''测试.php文件
<?php
use Project'Controller;
include('testcontroller.php');
$controller = new Controller();
$controller->show();
?>
我收到此错误...
Fatal error: Class 'Project'Registry' not found in PATH to file
除非我在控制器.class.php文件下面使用它
$this->registry = new 'MyLibrary'Registry;
因为在顶部的那个文件中,我有use Library'Registry;
我应该能够像这样访问它......
$this->registry = new Registry;
请帮我把它放在我可以这样使用它的地方
use Library'Registry;
namespace Project
{
我认为这是错误的方式:您use
全局命名空间中的Library'Registry
,然后打开Project
命名空间。
将 use
语句放在要将其导入到的命名空间中。
namespace Project
{
use Library'Registry;
Registry
类导入命名空间Project
因为您需要在那里,而不是在全局范围内。
<?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>';
}
}
}
?>
只需添加:使用 ''库''注册表;
在命名空间声明下的脚本顶部
然后你可以说:
$registry = 新注册表;
课堂内部
顺便说一下,你的类声明都是错误的。您不应该将命名空间包装在大括号内,命名空间不是一个函数。
这是应该的。还要确保通过使用 include('/path/to/registry.php' 包含库''注册表的类声明;或使用自动加载机
namespace Project;
include('E:''Library''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>';
}
}
享受
<?php namespace Project;
require_once 'your registry class file';
use 'Library'Registry as Registry;
现在您将能够使用...
$this->registry = new Registry;