首页 > 解决方案 > 致命错误:未捕获的错误:在 C:\apache\htdocs\inventory_manager\app\public\index.php:17 中找不到类“订单”

问题描述

我正在创建一个应用程序以在 composer.json 中使用自动加载来加载我的类文件。我得到 Fatal error: Uncaught Error: Class 'Order' not found in C:\apache\htdocs\inventory_manager\app\public\index.php:17

我在 vendor/composer/autoload_psr4.php 中检查了 $vendorDir 文件夹相对于我的 $baseDir 的路径,并且它是正确的。我已经检查过有类似问题的线程,但仍然存在。提前致谢

这是我的 composer.json 文件

{
  "name": "inventory manager",
  "description": "Inventory Management System",
  "require": {
    "phpmailer/phpmailer": "~6.0"
  },
  "autoload": {
    "psr-4": {
      "App\\": "app"
    }
  }
}

这是我的文件夹结构

 |---inventory_manager
     |---app
         |---Model
             |---DatabaseObject.php
             |---Order.php
             |---Product.php
     |---public
         |---assets
             |---css
         |---index.php
     |---vendor
     |---composer.json

订单.php

namespace App;

class Order
{
  private $name;
  private $brand;
  private $shape;


  function __construct($name,$brand, $shape)
  {
    $this->name = $name;
    $this->brand = $brand;
    $this->shape = $shape;
  }

  public function toString()
  {
    return "{$this->name}, {$this->brand}, {$this->shape}";
  }
}

索引.php

<?php
  require '../../vendor/autoload.php';

  $prdt = new App\Order('Civic', 'Honda', 'Classic');

  var_dump($prdt);

标签: phpcomposer-php

解决方案


您为Order班级设置了错误的命名空间。

替换namespace App;namespace App\Model;


推荐阅读