douhong4452 2018-10-17 19:42
浏览 170
已采纳

使用Composer自动加载PHP类

I am trying for the first time to autoload PHP Classes using Composer.

I have this dir structure:

-app
  -controllers
  -models
    -MySql.php
    -interfaces
      -IDatabase.php

My problem is that I am not able to implement the IDatabase interface in my MySql class. It gives me: Fatal error: Interface 'App\Models\I\IDatabase' not found.

composer.json

{
  "autoload": {
    "psr-4": {
      "App\\": "app/"
    }
  }
}

IDatabase.php

namespace App\Models\I;

interface IDatabase
{

  public function connect();
  public function execute($query,$param);
}

Mysql.php

namespace App\Models;

use App\Models\I\IDatabase;

class MySql implements IDatabase
{
...
}

What am I doing wrong? I can't figure it out. Thanks.

  • 写回答

2条回答 默认 最新

  • dongsashe6006 2018-10-17 19:56
    关注

    It looks like

    App\Models\I\IDatabase
    

    Should be

    App\Models\Interfaces\IDatabase
    

    You can't abbreviate the directory name.

    Also as I said in the comments

    Casing is important! app vs App, etc. or app\models\interfaces\IDatabase It's probably mainly this one I vs interfaces but Casing will get you on Linux.

    So you can either change the directories to be uppercase, or you can change the namespace to be lower cased (but they should be the same).

    On windows this may work with the case issues, because windows filesystem is case insensitive. But as soon as you put that on Linux which is case sensitive, well it's not gonna work out to good.

    Cheers!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
  • duanmu0834 2018-10-17 19:55
    关注

    Namespaces should follow the exact directory structure, and are case sensitive.

    If your Interface is in the directory app\models\interfaces, the namespace should be App\models\interfaces. App in your case can be with capital A because you explicitly mapped it in the composer.json.

    So you can either rename the folders or fix the namespace.

    Also, you cannot give custom name to the namespaces (i.e. I in App\Models\I). As I stated before, the namespace must follow the directory structure, unless you create a specific mapping in the composer.json

    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 我现在有一些关于提升机故障的专有文本数据,量也不多,我在label studio上进行了关系和实体的标注,完成了知识图谱的构造,那么我使用生成式模型的话,我能做哪些工作来写我的论文?
  • ¥15 电脑连不上无线网络如下诊断反馈应该如何操作
  • ¥15 telegram api 使用forward_messages方法转发消息时,目标群组里面会出现此消息来源,如何隐藏?
  • ¥15 在ubuntu中无法连接到远程服务器传输文件
  • ¥15 关于#tensorflow#的问题:有没有什么方法可以让机器自己学会像素风格的图片
  • ¥15 Oracle触发器字段变化时插入指定值
  • ¥15 docker无法进入容器内部
  • ¥15 qt https 依赖openssl 静态库
  • ¥15 python flask 报错
  • ¥15 改个密码引发的项目启动问题