Search code examples
phpcomposer-phpautoloadpsr-0psr-4

Composer autoload with different (shorter) directory structure


I want my PHP classes to be loaded by Composer. It works when I follow the directory structure, according to the namespaces.

For example, my class \MyCompany\MyProject\Class1 is loaded when located in src/MyCompany/MyProject/Class1.php. I use this composer settings:

"autoload": {
    "psr-0": { "MyCompany\\MyProject\\": "src/" }
}

However, I do not want to put all my files to MyCompany directory since it is the only one in src. I know Java does it like this, but still. Is there any way to set up Composer so it will load my classes when they are directly in src? For example from file src/Class1.php.

Edit: PSR-4 addresses directly this problem, although classmap still works (and it's recommended for production setup).


Solution

  • What about this?

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