Cannot find Class with PHP Namespace(找不到带有 PHP 命名空间的类)
问题描述
我之前发布了一些关于在 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 esting.php on line 6
E:Controller esting.php 文件
E:Controller esting.php File
<?php
use Controller;
include('testcontroller.php');
$controller = new Controller;
$controller->show();
?>
E:Controller estcontroller.php 文件
E:Controller estcontroller.php File
<?php
use LibraryRegistry;
namespace Controller
{
class Controller
{
public $registry;
function __construct()
{
include('E:LibraryRegistry.class.php');
$this->registry = new Registry;
}
function show()
{
echo $this->registry;
echo '<br>Registry was ran inside testcontroller.php<br>';
}
}
}
?>
E:LibraryRegistry.class.php 文件
E:LibraryRegistry.class.php File
<?php
namespace LibraryRegistry
{
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 ControllerController();
// Error, because in current scope there is no such class
$controller = new Controller();
$controller->show();
?>
另一个例子:
testcontoller.php:
testcontoller.php:
<?php
namespace SomePathToController;
class Controller
{
function __construct()
{
}
function show()
{
echo '<br>Was run inside testcontroller.php<br>';
}
}
?>
testing.php:
testing.php:
<?php
include('testcontroller.php');
use SomePathToController;
// We now can access Controller using only Controller namespace,
// not SomePathToController
$controller = new ControllerController();
// Error, because, again, in current scope there is no such class
$controller = new Controller();
$controller->show();
?>
如果您希望准确导入 Controller
class,您需要执行 use ControllerController
- 然后可以在您当前的范围.
If you wish to import exactly the Controller
class, you need to do use ControllerController
- then this class will be accessible in your current scope.
这篇关于找不到带有 PHP 命名空间的类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:找不到带有 PHP 命名空间的类
基础教程推荐
- 通过 PHP SoapClient 请求发送原始 XML 2021-01-01
- 超薄框架REST服务两次获得输出 2022-01-01
- 在多维数组中查找最大值 2021-01-01
- 在 Woocommerce 中根据运输方式和付款方式添加费用 2021-01-01
- 如何在 PHP 中的请求之间持久化对象 2022-01-01
- XAMPP 服务器不加载 CSS 文件 2022-01-01
- Libpuzzle 索引数百万张图片? 2022-01-01
- mysqli_insert_id 是否有可能在高流量应用程序中返回 2021-01-01
- 在 PHP 中强制下载文件 - 在 Joomla 框架内 2022-01-01
- WooCommerce 中选定产品类别的自定义产品价格后缀 2021-01-01