doumu4916 2011-12-22 23:10
浏览 149
已采纳

找不到带有PHP命名空间的Class

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.

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 File

<?php
use \Controller;

include('testcontroller.php');

$controller = new Controller;
$controller->show();
?>

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 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.

  • 写回答

5条回答 默认 最新

  • duanqiongniu1469 2011-12-23 09:33
    关注

    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

    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();
    ?>
    

    Another example:

    testcontoller.php:

    <?php
    namespace Some\Path\To\Controller;
    
    class Controller
    {
        function __construct()
        {
    
        }
    
        function show()
        {
            echo '<br>Was run inside testcontroller.php<br>';
        }
    }
    ?>
    

    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();
    ?>
    

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

悬赏问题

  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘