【发布时间】:2012-01-26 11:58:46
【问题描述】:
我之前发布了一些关于在 PHP 中使用命名空间的问题,从我得到的信息来看,我下面的这个示例代码应该可以工作。
但是,当我尝试像这样在 PHP 中使用命名空间时出现错误。这是按原样运行下面的代码时的第一个错误...
Fatal error: Class 'Controller' not found in E:\Controllers\testing.php on line 6
E:\Controller\testing.php 文件
<?php
use \Controller;
include('testcontroller.php');
$controller = new Controller;
$controller->show();
?>
E:\Controller\testcontroller.php 文件
<?php
use \Library\Registry;
namespace Controller
{
class Controller
{
public $registry;
function __construct()
{
include('E:\Library\Registry.class.php');
$this->registry = new Registry;
}
function show()
{
echo $this->registry;
echo '<br>Registry was ran inside testcontroller.php<br>';
}
}
}
?>
E:\Library\Registry.class.php 文件
<?php
namespace Library\Registry
{
class Registry
{
function __construct()
{
return 'Registry.class.php Constructor was ran';
}
}
}
?>
如您所见,我试图使其尽可能简单,只是为了让命名空间部分正常工作。我尝试了不同的变体,但似乎无法弄清楚。
【问题讨论】:
标签: php namespaces