2

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!

Renato Maretić
  • 315
  • 1
  • 4
  • 18
  • Does this answer your question? [PSR-4 autoloader Fatal error: Class not found](https://stackoverflow.com/questions/30136773/psr-4-autoloader-fatal-error-class-not-found) – rob006 Jun 02 '22 at 13:10

1 Answers1

0

After some research, I discovered solution. Adding:

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

to composer.json solved the issue.

Renato Maretić
  • 315
  • 1
  • 4
  • 18