Search code examples
composer-phpautoload

composer dump-autoload --optimize only load classes for one package


In the same project I have these two composer.json files:

{
    "name": "kingsoft/persist",
    "description": "Object Persistance",
    "type": "library",
    "license": "MIT",
    "autoload": {
        "psr-4": {
            "Kingsoft\\Persist\\": "src/"
        }
    },
    "require": {}
}

and

{
    "name": "kingsoft/http",
    "description": "Http Request/Response handler",
    "type": "library",
    "license": "MIT",
    "autoload": {
        "psr-4": {
            "Kingsoft\\Http\\": "src/"
        }
    },
    "require": {}
}

The folder structure is :

- vendor
  - kingsoft
    - http
      - src
        Request.php
      composer.json
    - persist
      - src
        Base.php
      composer.json

The problem is that only the Persist class is loaded, not the Request class.

What is the reason for autoload skipping the classes in the HTTP package?


Solution

  • Strict naming rules in PSR-4

    Although the PSR-4 standard is not entirely clear on that naming under PSR-4 is quit strict. I assume a not PSR-4 conform name problem. The namespace for Persist probably is Persist and works the one for http quite likely but incorrectly HTTP.

    Solution

    Rename the namespace of the sources in the http packages to Http.