user9418
user9418

Reputation: 395

PSR 4 autoload not finding class

I'm not sure why my PSR-4 auotloader isn't working.

My composer.json file is simply

{
  "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 and run the code I get

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

I'm not sure why it isn't working, I tried running composer update, composer install and composer dumpautoload but the error still appears.

My full working structure is the following

.
├── 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

Upvotes: 4

Views: 1364

Answers (1)

Pᴇʜ
Pᴇʜ

Reputation: 57683

Autoload section should be

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

Upvotes: 4

Related Questions