dongyingjiu0669 2017-02-04 09:07
浏览 33
已采纳

我得到命名空间错误

Am learning how namespace works and have three files:- index, iClass, and iClassed

iClass.php

<?php

namespace app\ic;

class iClass {
    public static function callMeFromClass() {
        echo 'OK - you have called me!';
        exit;
    }
}

iClassed.php

<?php

namespace app\icl;

class iClass {
    public static function callMe() {
        echo 'OK - iclassed - you have called me!';
        exit;
    }
}

and the index.php

<?php
namespace inex;

require_once 'iClass.php';
require_once 'iClassed.php';

use app\ic\iClass;
iClass::callMeFromClass();

use app\icl\iClass;
iClass::callMe();

The error that I get after I try to run is

Cannot use app\icl\iClass as iClass because the name is already in use in C:\xampp\htdocs amespace\index.php on line 10

Can somebody explain why the error.

  • 写回答

1条回答 默认 最新

  • douliu1092 2017-02-04 09:32
    关注

    Sure, you try to import two classes under the same local name iClass, that obviously is not possible. Either use the fully qualified class name (namespaced name) or import them under different names.

    Using a fully qualified class name would mean:

    \app\ic\iClass::callMe();
    \app\icl\iClass::callMe();
    

    Or you "use" the classes under a different local name:

    use app\ic\iClass;
    use app\icl\iClass as iClassed; 
    
    iClass::callMeFromClass();
    iClassed::callMe();`
    

    I suggest you start reading the documentation of the things you use. It is of great quality and comes with good examples: http://php.net/manual/en/language.namespaces.php

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 软件工程用例图的建立(相关搜索:软件工程用例图|画图)
  • ¥15 如何在arcgis中导出拓扑关系表
  • ¥15 处理数据集文本挖掘代码
  • ¥15 matlab2017
  • ¥15 在vxWorks下TCP/IP编程,总是connect()报错,连接服务器失败: errno = 0x41
  • ¥15 AnolisOs7.9如何安装 Qt_5.14.2的运行库
  • ¥20 求:怎么实现qt与pcie通信
  • ¥50 前后端数据顺序不一致问题,如何解决?(相关搜索:数据结构)
  • ¥15 基于蒙特卡罗法的中介效应点估计代码
  • ¥15 罗技G293和UE5.3