问题描述
我之前曾发布过一些有关在PHP中使用命名空间的问题,从我得到的内容来看,下面的示例代码应该可以正常工作.
I posted some questions previously regarding the use of Namespaces in PHP and from what I got, this example code I have below should be working.
但是,当我尝试像这样在PHP中使用命名空间时,出现错误.按原样运行以下代码时,这是第一个错误...
However I am getting errors when I try to use Namespace in PHP like this. Here is the first error when running the code below as is...
Fatal error: Class 'Controller' not found in E:\Controllers\testing.php on line 6
E:\ Controller \ testing.php文件
E:\Controller\testing.php File
<?php
use \Controller;
include('testcontroller.php');
$controller = new Controller;
$controller->show();
?>
E:\ Controller \ testcontroller.php文件
E:\Controller\testcontroller.php File
<?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文件
E:\Library\Registry.class.php File
<?php
namespace Library\Registry
{
class Registry
{
function __construct()
{
return 'Registry.class.php Constructor was ran';
}
}
}
?>
如您所见,我试图使它尽可能简单,只是为了使命名空间部分正常工作.我尝试了不同的变体,但似乎无法弄清楚.
As you can see I tried to make it as simple as possible just to get the Namespace part working. I have tried different variations and cannot seem to figure it out.
推荐答案
即使使用use
语句,也需要指定要实例化的类的名称空间.这里有很多示例: http://www.php.net /manual/en/language.namespaces.importing.php
Even when using use
statement, you need to specify the namespace of the class you are trying to instantiate. There are a lot of examples here: http://www.php.net/manual/en/language.namespaces.importing.php
为了更好地理解它,我将向您介绍它的工作原理.就您而言,当您执行use \Controller
时,整个Controller
命名空间将变为可用,但该命名空间中的类将不可用.因此,例如:
To understand it better, I will describe to you how it works. In your case, when you do use \Controller
, the whole Controller
namespace becomes available to you, but not the classes that are in this namespace. So, for example:
<?php
include('testcontroller.php');
use \Controller;
// Desired class is in namespace!
$controller = new Controller\Controller();
// Error, because in current scope there is no such class
$controller = new Controller();
$controller->show();
?>
另一个例子:
testcontoller.php:
testcontoller.php:
<?php
namespace Some\Path\To\Controller;
class Controller
{
function __construct()
{
}
function show()
{
echo '<br>Was run inside testcontroller.php<br>';
}
}
?>
testing.php:
testing.php:
<?php
include('testcontroller.php');
use \Some\Path\To\Controller;
// We now can access Controller using only Controller namespace,
// not Some\Path\To\Controller
$controller = new Controller\Controller();
// Error, because, again, in current scope there is no such class
$controller = new Controller();
$controller->show();
?>
如果您想完全导入Controller
类,则需要执行use Controller\Controller
-这样的类将在您当前的范围内可用.
If you wish to import exactly the Controller
class, you need to do use Controller\Controller
- then this class will be accessible in your current scope.
这篇关于找不到带有PHP命名空间的类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!