PSR 4 startup does not find class

I am not sure why my PSR-4 auotloader is not working.

My composer.json file is just

{
  "require" : {
     "abraham/twitteroauth" : "*"
  },
  "autoload" : {
    "psr-4" : {"Filters\\" : "src"}
  }
}

My PHP file, which is located in src / Filters

namespace Filters;

class BlogFilter {

  public function __construct()
  {
    return 'hello from the constructor';
  }

}

In my main file, located in the root, I have the following

require 'vendor/autoload.php';

use Filters\BlogFilter;

$foo = new BlogFilter();

echo $foo;

But when I try to run the code, I get

Fatal error: Class 'Filters \ BlogFilter' not found in /var/www/html/dev/foo.php on line 7

I am not sure why it does not work, I tried to start composer update, composer installand composer dumpautoload, but the error still appears.

My full working structure is as follows

.
├── composer.json
├── composer.lock
├── foo.php
├── src
│   ├── Filters
│   │   └── BlogFilter.php
│   └── TestDir
└── vendor
    ├── abraham
    │   └── twitteroauth
    │       ├── autoload.php
    │       ├── composer.json
    │       ├── LICENSE.md
    │       ├── phpunit.xml
    │       ├── README.md
    │       ├── src
    │       │   ├── Util
    │       │   │   └── JsonDecoder.php
    │       │   └── Util.php
    │       └── tests
    ├── autoload.php
    └── composer
        ├── autoload_classmap.php
        ├── autoload_namespaces.php
        ├── autoload_psr4.php
        ├── autoload_real.php
        ├── ClassLoader.php
        └── installed.json
+4
source share
1 answer

Startup section should be

  "autoload" : {
    "psr-4" : {"Filters\\" : "src/Filters"}
  }
+3
source

All Articles