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.
In this repository119 other files it gives its agents.
- strategy-bitrix-d7-incrementalSkill
- strategy-explicit-over-magicSkill
- strategy-framework-conventionsSkill
- strategy-legacy-safe-editsSkill
- strategy-minimal-diffSkill
- strategy-no-untested-refactorSkill
- strategy-preserve-public-apiSkill
- strategy-readonly-immutabilitySkill
- strategy-security-firstSkill
- strategy-test-drivenSkill
- openapiSkill
- paginationSkill
- rest-designSkill
- versioningSkill
- cqrsSkill
- dependency-inversionSkill
- hexagonalSkill
- repository-patternSkill
- service-layerSkill
- bitrixSkill
- findings-reportSkill
- laravelSkill
- performanceSkill
- phpSkill
- project-analysisSkill
- refactoringSkill
- roi-prioritizationSkill
- securitySkill
- stack-detectionSkill
- cache-layersSkill
- component-engineSkill
- d7-eventsSkill
- d7-loaderSkill
- d7-orm-queriesSkill
- iblock-elementsSkill
- kernel-main-moduleSkill
- localizationSkill
- controllersSkill
- ormSkill
- validationSkill
- controllersSkill
- query-builderSkill
- routingSkill
- dependency-managementSkill
- scriptsSkill
- connection-poolingSkill
- mysql-indexingSkill
- redisSkill
- transactionsSkill
- ci-github-actionsSkill
- deployment-strategiesSkill
- docker-phpSkill
- env-configSkill
- associationsSkill
- entities-mappingSkill
- orm-basicsSkill
- entity-apiSkill
- module-developmentSkill
- routing-controllersSkill
- formsSkill
- The first 60 shown.
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
- Very specific: line 62
Do not use this skill for a project that does not use Composer.
- Distinctive: line 63
Do not rely on this skill to load a class whose file path is computed at runtime outside any namespace convention.
- Very specific: line 77
Add an autoload.psr-4 entry mapping your root namespace to its source directory, e.g.
- Very specific: line 79
Run composer dump-autoload so Composer regenerates the loader with the new mapping.
- Very specific: line 85
After editing any autoload block, run composer dump-autoload to regenerate the loader.
- Distinctive: line 113
Not applicable — no I/O, auth, or untrusted input.
≈ “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.
Files
<!-- 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_registercallback 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 thanautoload. - Map each namespace prefix to one base directory — In the
autoload.psr-4block, map a namespace prefix ending in a backslash to a single base directory, so"App\\": "src/"resolvesApp\Http\Kerneltosrc/Http/Kernel.php. - Choose PSR-4 for conventional code, classmap for legacy — Use
psr-4for code that follows the namespace-to-path convention, and reserveclassmapfor legacy directories where file layout does not match namespaces.
Quick recipe
- Add an
autoload.psr-4entry mapping your root namespace to its source directory, e.g."App\\": "src/". (verify: composer.json contains theApp\\->src/mapping underautoload.psr-4) - Run
composer dump-autoloadso Composer regenerates the loader with the new mapping. (verify: A freshvendor/composer/autoload_psr4.phpincludes the new prefix) - Require
vendor/autoload.phpand 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-autoloadto regenerate the loader; PSR-4 code picks up new classes automatically, but changed mappings do not - For production, run
composer dump-autoload --optimize --classmap-authoritativeto convert PSR-4 lookups into a static classmap and skip filesystem probing
Anti-patterns
| Anti-pattern | Why it fails | Do instead |
| Using classmap for conventionally structured code | A 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 autoload | Every 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 classes | Manual 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
- 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. - 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. - 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-autoloadafter 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
--optimizebehavior so--classmap-authoritativeimplies 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 -->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
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.

