doukefu1361 2016-09-20 15:12
浏览 46
已采纳

PHP命名空间错误。 在作曲家项目中找不到的类

Can someone please point me in the right direction. I have downloaded a GIT project Namshi/JOSE which is a JWT signing and verification library. I have a directory structure shown below.

directory structure

All my unit tests (inside the folder tests work perfectly, so the composer.json and bootstrap.php files are okay. But when I try and add my own test, in the folder myTests->simpleTest.php as below what ever I try I can't get the namespace to resolve the class SimpleJWS. The namespace for the library is Namshi/JOSE. My simple test calling code is:

    <?php

      use Namshi\JOSE\SimpleJWS;
      //require_once ("../src/Namshi/JOSE/SimpleJWS.php");

      $jws  = new SimpleJWS(array(
       'alg' => 'RS256'
       ));

The error text is: Fatal error: Class 'Namshi\JOSE\SimpleJWS' not found. The psr-4 statements are as follows:

    "autoload": {
    "psr-4": {
    "Namshi\\JOSE\\":  "src/Namshi/JOSE/"

    }
    },
    "autoload-dev": {
    "psr-4": {
        "Namshi\\JOSE\\Test\\":  "tests/Namshi/JOSE/Test/"

    }

Any help would be greatly appreciated.

  • 写回答

1条回答 默认 最新

  • douyi6960 2016-09-20 15:58
    关注

    Your thinking about the autoloading is correct, but as you're stating, your test file lives under "myTests"; your autoload-dev simply points to the wrong directory "tests/Namshi/JOSE/Test/" while your file lies in "myTests".

    "autoload-dev": {
        "psr-4": {
             "Namshi\\JOSE\\Test\\": "myTests"
        }
    }
    

    Please also make sure, your FQCN (fully-qualified class name) of your test class is Namshi\JOSE\Test\simpleTest.

    <?php
    
    namespace Namshi\JOSE\Test;
    
    class simpleTest {}
    

    According to the comments below you were missing the include of the autoloading in your testsuite.

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

报告相同问题?

悬赏问题

  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵