0
votes

I'm creating an app to use autoload in composer.json to load my class files. I was getting Fatal error: Uncaught Error: Class 'Order' not found in C:\apache\htdocs\inventory_manager\app\public\index.php:17

I checked the path of the $vendorDir folder relative to my $baseDir in the vendor/composer/autoload_psr4.php and its correct. I've checked through threads with similar issues but still. Thanks in advance

This is my composer.json file

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

This is my folder Structure

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

Order.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}";
  }
}

index.php

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

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

  var_dump($prdt);
1
Does index.php have a namespace? (in case it was edited out for this example)Nicholas Summers
the public dir is within the app dirT.melz
Also it looks like the order class needs to be in the \app\Model\Order namespace.Nicholas Summers
No please @NicholasSummersT.melz
Yes it is @NicholasSummersT.melz

1 Answers

0
votes

You have the wrong namespace set for your Order class.

Replace namespace App; with namespace App\Model;