agentleFS
AnyOrgNameAnyOrgName/php-skills/skills/composer/autoload-psr4/SKILL.md

autoload-psr4

Skill4 starsChanged 4 months agoMITView on GitHub

Configure class autoloading in composer.json — PSR-4 namespace mapping, classmap, and files autoload — and regenerate the optimized autoloader. Use when the user hits a "class not found" error and needs to fix the autoload mapping or regenerate the autoloader; the user needs to always load a file of global helper functions or constants that has no class to autoload; the user wants Composer to autoload classes by mapping a namespace prefix to a source directory. Keywords: composer, autoload, psr4, classmap, namespaces.

The file

We split this skill into the rules it gives an agent

6 rules. About 0 are common, 2 are distinctive, and 4 are very specific to this project.

  • ≈0

    Common · estimate

    Many files like this have a rule of this kind.

  • ≈2

    Distinctive · estimate

    This author's own choices. Worth a look.

  • 4

    Very specific

    Tied to their project's names, files or tools. Probably skip.

  • Common
  • Distinctive
  • Very specific
  1. Very specific:

    Do not use this skill for a project that does not use Composer.

    line 62
  2. Distinctive:

    Do not rely on this skill to load a class whose file path is computed at runtime outside any namespace convention.

    line 63
  3. Very specific:

    Add an autoload.psr-4 entry mapping your root namespace to its source directory, e.g.

    line 77
  4. Very specific:

    Run composer dump-autoload so Composer regenerates the loader with the new mapping.

    line 79
  5. Very specific:

    After editing any autoload block, run composer dump-autoload to regenerate the loader.

    line 85
  6. Distinctive:

    Not applicable — no I/O, auth, or untrusted input.

    line 113

≈ “Common” is an estimate: at least 1 in 5 of the 350,495 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.

Keep what fits and drop the rest. Your version, ready for your agents.
Files
SKILL.md
<!-- generated: do not edit — source: knowledge/atoms/composer/autoload-psr4/ --><!-- schema: atomVersion=3 skillVersion=2 graphVersion=1 --><!-- compile: @php-skills/compiler v1.0.0 (deterministic build) -->

Composer PSR-4 Autoloading

When to use

  • The user hits a "class not found" error and needs to fix the autoload mapping or regenerate the autoloader.
  • The user needs to always load a file of global helper functions or constants that has no class to autoload.
  • The user wants Composer to autoload classes by mapping a namespace prefix to a source directory.

When NOT to use

  • Do not use this skill for a project that does not use Composer; register a manual spl_autoload_register callback instead.
  • Do not rely on this skill to load a class whose file path is computed at runtime outside any namespace convention.

Core guidance

  • Put test namespaces under autoload-dev — Map test and fixture namespaces under autoload-dev.psr-4 (for example "Tests\\": "tests/") rather than autoload.
  • Map each namespace prefix to one base directory — In the autoload.psr-4 block, map a namespace prefix ending in a backslash to a single base directory, so "App\\": "src/" resolves App\Http\Kernel to src/Http/Kernel.php.
  • Choose PSR-4 for conventional code, classmap for legacy — Use psr-4 for code that follows the namespace-to-path convention, and reserve classmap for legacy directories where file layout does not match namespaces.

Quick recipe

  1. Add an autoload.psr-4 entry mapping your root namespace to its source directory, e.g. "App\\": "src/". (verify: composer.json contains the App\\ -> src/ mapping under autoload.psr-4)
  2. Run composer dump-autoload so Composer regenerates the loader with the new mapping. (verify: A fresh vendor/composer/autoload_psr4.php includes the new prefix)
  3. Require vendor/autoload.php and instantiate a mapped class to confirm resolution, e.g. new App\Http\Kernel(). (verify: The class instantiates without a class-not-found error)

Best practices

  • After editing any autoload block, run composer dump-autoload to regenerate the loader; PSR-4 code picks up new classes automatically, but changed mappings do not
  • For production, run composer dump-autoload --optimize --classmap-authoritative to convert PSR-4 lookups into a static classmap and skip filesystem probing

Anti-patterns

Anti-patternWhy it failsDo instead
Using classmap for conventionally structured codeA classmap must be regenerated on every new class, so developers forget to dump-autoload and hit spurious class-not-found errors.Use psr-4 for convention-following code so new classes autoload without a rescan; keep classmap for legacy layouts only.
Loading class-heavy code through the files autoloadEvery files entry is included on every request whether used or not, adding startup<br>cost and defeating lazy autoloading.Reserve files for global helpers and constants, and let classes load lazily through psr-4.
Hand-writing require statements for your own classesManual requires break as files move, create load-order bugs, and duplicate the mapping the autoloader already provides.Map the namespace under autoload.psr-4 and rely on Composer's generated autoloader.

See references/anti-patterns.md.

Common mistakes

  1. Directory or file case does not match the class name — A file or directory whose case differs from the class or namespace segment fails to autoload on case-sensitive filesystems. Fix: Rename files and directories to match the class and namespace segment casing exactly, then run composer dump-autoload.
  2. PSR-4 prefix missing its trailing separator — A PSR-4 key without a trailing namespace separator can shadow sibling namespaces or fail to match. Fix: Always end a PSR-4 namespace key with an escaped backslash, e.g. "App\\", and re-dump the autoloader.
  3. Forgetting to dump the autoloader after a mapping change — Changing a namespace mapping without regenerating the autoloader leaves the old resolution in place. Fix: Run composer dump-autoload after any edit to the autoload configuration in composer.json.

Version notes

  • Composer 2 changes optimized autoloader flags (PHP >=7.4) — Composer 2 splits the old --optimize behavior so --classmap-authoritative implies optimization and skips the filesystem fallback entirely.

Security

  • Not applicable — no I/O, auth, or untrusted input.

Testing

  • Activation and decision behavior are verified in eval/scenarios/composer/autoload-psr4.yaml.

Additional resources

  • references/anti-patterns.md
  • references/core.md
  • references/examples.md
  • references/pitfalls.md
  • references/recipes.md
  • references/version-differences.md

Knowledge graph

Related: composer/dependency-management, psr/psr4-autoloading

<!-- graph-fragment: graph/fragments/composer/autoload-psr4.yaml -->

Download now (ZIP)

Does it work?

Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.

No reviews yet

People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.

  • It worked
  • Partly
  • It didn't
What did you use it for? e.g. Reviewing pull requests in a Rails app

Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.

Your trail

Files you open will collect here, so you can find them again.

Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.

Now do this for your own.

Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.