PHP Fatal error: Class 'MyApp\Chat' not found in /MyApp/chat-server.php

If you don't have the composer.json file and the vendor folder at root, it won't work. Also, don't touch the vendor folder (and subfolders). The folder structure must be like this:

/composer.json
/composer.phar
/vendor/[misc]
/src/MyApp/Chat.php

Finally, after updating the composer.json, make sure to run an update:

php composer.phar update

This way it will work.


The main path for the autoloading is the location of the composer.json file, so if that lives in /var/www/src/MyApp/, the autoloading will use that as a base.

In your case, you say the MyApp namespace can be found in the src directory (which means /var/www/src/MyApp/src). That's not true, since the file /var/www/src/MyApp/src/MyApp/Chat.php does not exists.

You can solve this issue in 3 different ways:

  • Moving composer.json - You can move the composer.json file to /var/www, to be able to use that as base;
  • Updating autoloading (using PSR-4) - You can also use PSR-4 instead of PSR-0 and configure it as "autoload": { "psr-4": { "MyApp\\": "" } };
  • Reorganizing your files - You can also change the file structure to:

    /var/www/src/MyApp/
        src/MyApp/
                Chat.php
        composer.json