【问题标题】:Composer PSR-4 Autoloader Class not foundComposer PSR-4 Autoloader 类未找到
【发布时间】:2017-06-10 11:58:39
【问题描述】:

这是我的文件结构

├── index.php
├── composer.json
├── src
    ├── Core
        └── Database.php
├── vendor

在我的 composer.json 文件中

    "autoload": {
        "psr-4": {"Site\\": "src/"}
    }

在我的 Database.php 中

namespace Core;

class Database {}

在我的 index.php 中

 $db = new Site\Core\Database();

但我收到一个错误:找不到类 'Site\Core\Database'。

我已经运行了composer updatecomposer dump-auto -o,但仍然没有成功。我已经查看了这里的许多其他类似问题,但它们没有帮助。

谢谢

【问题讨论】:

  • 您的 Database 类就在 Core 命名空间中。更改您的作曲家条目以从 src/ 加载 Core 或将 Site\ 添加到您的数据库命名空间。

标签: php composer-php


【解决方案1】:

当您在composer.json 中这样定义psr-4

"autoload": {
    "psr-4": {"Site\\": "src/"}
}

这意味着Autoloader 将在src/Core/Database.php 中查找文件Site\Core\Database

但请确保在 Database.php 中定义 namespace

namespace Site\Core;

class Database
{
}

问题是,你只需定义namespace Core;

【讨论】:

  • 我更新了我的命名空间,但仍然出现同样的错误。
  • @ccKep,已修复。这是由于一个错字。
  • @FrenchMajesty 你是怎么解决的?我有同样的问题,我尝试了不同的命名空间,但我总是收到错误 Class 'Foo\Bar\Core\User' not found
猜你喜欢
  • 2015-12-16
  • 1970-01-01
  • 1970-01-01
  • 2018-06-01
  • 2017-11-20
  • 2017-02-01
  • 2015-02-17
  • 2016-03-22
  • 2019-04-20
相关资源
最近更新 更多