Home > other >  "Class not found" when trying to use composer auto load in a wordpress plugin
"Class not found" when trying to use composer auto load in a wordpress plugin

Time:05-30

I have problem that occurs on a rare occasions. On some PCs, and WordPress instances class loading using composer works, but on a rare occasions I can't load any classes, and I'm getting error Class not found. I noticed that when it works, it's usually Mac or a Windows machine, run from either Docker container, Local WP, or XAMPP.

composer.json looks like this:

{
    "name": "vendor/my-plugin",
    "license": "proprietary",
    "description": "Integration of the ...",
    "autoload": {
        "psr-4": {
            "Vendor\\MyPlugin\\": "src"
        }
    },
    "require": {
        "jumbojett/openid-connect-php": "^0.9.5"
    }
}

Folder structure is similar to this one:

myplugin.php
composer.json
composer.lock
vendor
  - ...
src
  - controller
    - Settings.php
  - enumerators
    - Message.php
    - Uri.php
  - helper
    - Template.php
  - view
    ...

Running composer install goes without any errors. Part of myplugin.php file where I'm registering namespace and loading class looks like this:

<?php

namespace Vendor\MyPlugin;

require_once __DIR__ . '/vendor/autoload.php'; 

use Vendor\MyPlugin\Helper\Template;
use Vendor\MyPlugin\Controller\Settings;
use Vendor\MyPlugin\Enumerators\Uri as UriEnumerator;

I use these classes later on in the code, but as I said, it doesn't recognize classes on some instances of the Wordpress, but it works on ~70% of computers / Wordpress instances.

I am thankful for any suggestion!

CodePudding user response:

After some research, I discovered solution. Adding:

"config": {
        "optimize-autoloader": true
    }

to composer.json solved the issue.

  • Related