-2

I have a very simple PHP/Composer application with the following structure:

- src
  - content
    - test
      - Sandbox.php

Sandbox.php has only a static function to print "test" and its namespace is

namespace MyApplication\Content\Test;

My autoload.php has MyApplication an "autoload" property.

"autoload" : {
    "psr-4": {"MyApplication\\": "src/"}
},

I run composer install --no-dev in an Windows environment with XAMPP and in a test.php file I do (for the sake of a very simple test):

$autoloadFile = __DIR__ . '/wp-content/plugins/sandbox/vendor/autoload.php';
require $autoloadFile;
echo 'autoload = ' . file_exists($autoloadFile);
echo '<br />';
echo 'class_exists = ' . class_exists('MyApplication\Content\Test\Sandbox');

When I run this test.php file locally, it works perfectly. MyApplication is loading Sandbox class.

However, when I release this to my server which is a Linux based server but running on the same PHP version, Sandbox class is not found.

I made sure my /vendor/ folder is properly uploaded as well.

I'm wondering if the problem is happening because I'm running composer install on a Windows environment while it should be running the same command in my server (which I can't at the moment). Shouldn't the /vendor/ folder upload be enough to make the autoload classes work well?

Adriano Castro
  • 1,411
  • 4
  • 19
  • 33
  • 1
    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 May 28 '20 at 15:43
  • What have you tried to debug the problem? – Nico Haase May 29 '20 at 06:38
  • @rob006 unfortunately, no. I've tried that before. It's weird because in my development environment it works, but when I release it to another server it won't. – Adriano Castro Jun 01 '20 at 15:42
  • @nico-haase the only thing I can debug at the moment is checking error_log file, which tells me my class was not found. I wish this same error could happen locally. That way I'd have more tools to debug. – Adriano Castro Jun 01 '20 at 15:42

1 Answers1

3

Path to your file is src/content/test/Sandbox.php and according to PSR-4 it should be src/Content/Test/Sandbox.php - on Windows it does not matter, but on Linux it does.

kuba
  • 3,670
  • 3
  • 31
  • 41