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

JavaScript

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

JavaScript

Contents of test.php

JavaScript

Contents of Tire.php

JavaScript

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:

JavaScript

Your directory structure should be:

JavaScript

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:

JavaScript

then you have:

JavaScript

and test.php

JavaScript
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement