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!

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

报告相同问题?

悬赏问题

  • ¥15 脱敏项目合作,ner需求合作
  • ¥15 脱敏项目合作,ner需求合作
  • ¥30 Matlab打开默认名称带有/的光谱数据
  • ¥50 easyExcel模板 动态单元格合并列
  • ¥15 res.rows如何取值使用
  • ¥15 在odoo17开发环境中,怎么实现库存管理系统,或独立模块设计与AGV小车对接?开发方面应如何设计和开发?请详细解释MES或WMS在与AGV小车对接时需完成的设计和开发
  • ¥15 CSP算法实现EEG特征提取,哪一步错了?
  • ¥15 游戏盾如何溯源服务器真实ip?需要30个字。后面的字是凑数的
  • ¥15 vue3前端取消收藏的不会引用collectId
  • ¥15 delphi7 HMAC_SHA256方式加密