1
votes

I have a base folder with a vendor folder which include composer folder and my created folder src. The src folder contain all classes required to work.

In src folder, I have class example below

<?php

    namespace Test;

    class Example{
        public function display(){
            echo "This is Example Class";
        }
    }

 ?>

my composer.json has following content:

{
    "autoload":{
        "psr-4":{
            "Test\\":"src"
        }
    }   
}

and finally I am creating the instance of example class in a test file in base folder:

<?php

    require 'vendor/autoload.php';

    $a=new Test\Example();
    $a->display();


?>

The issue is when i try to run test file the following error occurred: [Fatal error: Uncaught Error: Class 'Test\Example' not found.]

Please help me what I am doing wrong, Here is the screen shot of directory structure. Thanks in advance.

1
You have src inside of vendor directory?rob006
Yes insider vendor directoryHasnain Abid Khanzada
vendor directory is only for dependencies and should be fully managed by Composer - at any point you should be able to remove it and restore it by calling composer install. Don't put your own files inside of it.rob006
Yes thanks, i put the src folder in base directory and it worked for me.Hasnain Abid Khanzada

1 Answers

3
votes

To be honest I'm not sure where your src, testing.php and composer.json are located, but they should be located in main directory (in your case 2-Basic-example and not in vendor directory). Then just run:

composer dump-autoload

and it should work assuming your Example class is saved as Example.php in src directory