未找到Composer PSR-4 Autoload类

时间:2017-12-19 01:14:04

标签: php namespaces composer-php autoload psr-4

我一直在打我的头几个小时,想弄清楚为什么自动加载不适用于" Authentication \ auth()"。 " dBase \ db()"上课正好装,但我得到了:

  

错误:Class' Authentication \ auth'找不到   第8行的/var/htdocs/dev/test.php

调用test.php。

file_structure

Root composer.json -

  "require": {
    "geeshoe/dbClass": "dev-develop",
    "geeshoe/authClass": "dev-master"
  },
  "autoload": {
    "psr-4": {
      "dBase\\": "vendor/geeshoe/dbclass/",
      "Authentication\\": "vendor/geeshoe/authClass/"
    }
  }

authClass.php标题 -

<?php
namespace Authentication;

use dBase\db;

class auth extends db
{

test.php -

if (file_exists("vendor/autoload.php")) {
    require "vendor/autoload.php";
} else {
    echo "Dam.. Something went wrong!";
}
$test = new \dBase\db();
$var = new \Authentication\auth();

如果有人可以向我指出明显的事情,那就太好了。另请注意,authClass-&gt; composer.json文件中未指定自动加载用于测试目的。

1 个答案:

答案 0 :(得分:4)

The problem here is that in fact you don't use PSR-4. In PSR-4 class name should match file name. For db class its fine because db class is located in db.php file, but auth class is located in authClass.php file and that's the problem. You should update file name to auth.php

You might need to run:

composer dump-autoload

Also keep in mind in real packages, one vendor package has one namespace, so you don't create multiple namespaces for single package but only single