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

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

图片转代码服务由CSDN问答提供 功能建议

我第一次尝试使用Composer自动加载PHP类。

我有这个目录结构:

  -app 
 -controllers 
 -models 
 -MySql.php 
 -interfaces 
 -IDatabase.php 
 <  / code>  
 
 

我的问题是我无法在 MySql 类中实现 IDatabase接口。 它给了我:致命错误:未找到接口'App \ Models \ I \ IDatabase'

composer.json

  {
“autoload”:{
“psr-4”:{
“App \\”:“app /”
} 
} 
}  
   
 
 

IDatabase.php

 命名空间App \ Models \ I; 
  
接口IDatabase 
 {
 
公共函数connect(); 
公共函数执行($ query,$ param); 
} 
   
 
 

< strong> Mysql.php

 命名空间App \ Models; 
 
use App \ Models \ I \ IDatabase; 
 
class MySql实现IDatabase 
 {  
 ... 
} 
   
 
 

我做错了什么? 我无法弄清楚。 谢谢。

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

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条)

相关推荐 更多相似问题