Skip to content
Advertisement

Why this Simple case of psr-4 not working with composer

I am trying to understand how psr-4 works using composer. These are the contents of my composer.json file

{
    "autoload": {
        "psr-4": {
            "Vehicle\Car\":"src/"
        }
    }
}

The Folder Structure is given below (please note I am using windows 10, so directory name casing should not matter I think) The folder where I have created ‘vendor’ folder is inside D:tempcomposer_test

D:
  temp
  composer_test
    test.php
    composer.json
    composer.lock
    vendor
      vehicle
        car
          src
            Tire.php
      

Contents of test.php

require __DIR__ . '/vendor/autoload.php';
$tire = new VehicleCarTire();

Contents of Tire.php

<?php
namespace VehicleCar;
class Tire {
    public function __construct()
    {
        echo "initialize Tiren";
    }
}

But when I run the code (snapshot below) . I see error

D:tempcomposer_test>php test.php

PHP Fatal error: Uncaught Error: Class ‘VehicleCarTire’ not found in D:tempcomposer_testtest.php:3 Stack trace: #0 {main} thrown in D:tempcomposer_testtest.php on line 3

I Don’t know what is wrong I am doing. Problem is when I install any other standard package I can use it successfully which seems like using the same format

Advertisement

Answer

Your composer.json should be:

{
    "autoload": {
        "psr-4": {
            "Vehicle\":"src/vehicle/"
        }
    }
}

Your directory structure should be:

D:
  temp
  composer_test
    test.php
    composer.json
    composer.lock
    src
      vehicle
        car
            Tire.php

Make sure to run composer dump-autoload to re-general the autoload files.

Also your namespace does not make much sense, naming your namespace Vehicle would mean that you only have vehicles in your repo. Usually you would name your namespace based on your project name, could be your brand or your username, or something more generic such as App.

So this would make more sense:

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

then you have:

D:
  temp
  composer_test
    test.php
    composer.json
    composer.lock
    src
      vehicle
        car
            Tire.php
      plane
        ...

and test.php

require __DIR__ . '/vendor/autoload.php';
$tire = new AppVehicleCarTire();
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement