Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

stefanbauer's avatar

General Composer PSR-4 Problem

Hi,

first of all. It is not Laravel specific, it is a general problem. I knew composer very well (i think) and using it for a long time. But now i ran into an issue, that i can not explain myself, but i am sure, that someone here, knows what to do :)

I am on a very old project of a customer and i wanted to use composer to autoload my files. Here is the basic (relevant) structure.

/application
/application/Test.php
/application/Whatever.php
/public
/public/index.php

I configured composer with PSR-4 to use Acme\ in application/. When i have a look at the generated autoload files, everything links fine. Require the autoloader and so on.

Here is the Problem:

If i give the "Test.php" the namespace Acme and call in index.php

new \Acme\Test; 

everything is working.

If the classname is NOT "Test" (for e.g. "Whatever"), it is not working. Always get the error, that the class could not be found. And now i am thinking.. "WHAAT" ? .. Really. i can choose whatever i want as name. Nothing is working. But the classname "Test" is working. Does anyone have an idea? I never had this problems before and have really no clue about it.

0 likes
8 replies
JarekTkaczyk's avatar

@stefanbauer

  1. Are you chaning class Test to class Whatever in a file called Test.php ?
  2. Are you calling composer dump-autoload after changing filename and class name ?
stefanbauer's avatar

@JarekTkaczyk

  1. No, i created separate file of course for it. For e.g. Whatever.php
namespace Acme;

class Whatever {

    public function __construct()
    {
        echo 'called';
    }

}
  1. Yes
stefanbauer's avatar
{
    "name": "dummy/dummy",
    "description": "dummy desc",
    "authors": [
        {
            "name": "Dummy",
            "email": "dummy@mail.com"
        },
    ],
    "autoload": {
        "psr-4": {
            "Acme\\": "application/"
        }
    },
    "minimum-stability": "stable",
    "require": {
    }
}

stefanbauer's avatar

@JarekTkaczyk Do you think it make sense to remove empty vendor folder and composer stuff and init it again? Maybe this helps. Because in my opinion everything seems right. Isn't it?

stefanbauer's avatar

@JarekTkaczyk Yeah i tried composer update several times. Didn't help. I did now a complete reinit composer from scratch. Now everything is working as expected. Thank you! But nevertheless.. strange behavior before. Never had that. Thanks!

amjad1233@gmail.com's avatar

I know it might sound stupid but I resolved it by adding classmap parameter after autoloading. as following

"autoload": {    
        "classmap": [
            "app"
        ]
    }

Please or to participate in this conversation.