Fatal error: Class not found simple autoloading with php and composer

Fatal error: Class not found simple autoloading with php and composer

我有一个像这样的简单设置结构:

/lib/
    / Car/Honda.php
    / MyClass.php
/vendor/composer/
/vendor/autoload.php
/composer.json
/index.php

作曲家.json就是这样

{
    "require": {},
    "autoload": {
      "psr-4": {
        "": "./lib/"
      }
    }
}

我的index.php就是这样

<?php

require_once('./vendor/autoload.php');
$new = new \Car\Honda(); // this one not found
//  $new = new MyClass() // this one works

我没有在 composer.json 中添加命名空间,因为我只是在试验。我确实添加了

"App\": "lib/"

整个事情都不行。我在这里错过了一些非常简单的东西吗???

注意:还尝试了 composer dump 和 composer dump-autoload。仍然无法正常工作...

Fatal error: Uncaught Error: Class 'Car\Honda' not found in /var/www/html/index.php:4 Stack trace: #0 {main} thrown in /var/www/html/index.php on line 4

试试这个

"autoload": {
    "classmap": [
        "lib/"
    ],
    "psr-4": {
    }
},

我明白了。大多数在线示例,设置命名空间 psr-4 {"namespace//" : "library_folder"} 像这样。

对于这个,我没有在 composer.json 文件中指定命名空间。

在您的 class 中,始终指定文件夹的命名空间。

/lib/Car/Toyota/Camry.php

namespace Car/Toyota; // this is the one missing.
class Camry {
  ...
}