Autoloader resulting in class not found

admin

Administrator
Staff member
I am trying to include an autoloader in my wordpress project. More specifically, I am developing a plugin that contains a Classes/ directory of all my classes. I want these classes to be accessible by namespace to my wordpress project root and children files/folders.

I feel like my composer.json should take care of the autoloader implementation, although I am still getting a Class not found fatal error. Has anyone else run into this issue? I appreciate any suggestions in advance!

This is what I tried so far:

./composer.json

Code:
  {
        "autoload": {
            "psr-4": {
                "Classes\\": "wp-content/plugins/example-plugin/Classes"
            }
        },
        "require": {}
    }

then...

Code:
composer install

./index.php (in wordpress root)

Code:
require_once('./vendor/autoload.php');

$foo = new \Classes\MyService();

var_dump($foo); //Fatal error: Uncaught Error: Class 'Classes\MyService' not found
die();

./wp-content/plugins/example-plugin/Classes/MyService.php

Code:
namespace Classes;

class MyService {