How PHP Autoloading Works
The PHP autoloader automatically includes a class file on first use. Without it, you would need manual require for every file. Composer generates the autoloader from your composer.json rules.
Autoloading Types
| Type | Description | Performance |
|---|---|---|
PSR-4 | Namespaces → directories | Good |
classmap | Pre-built class→file map | Best (production) |
files | Always-included files | — |
Optimize Autoloader for Production
composer dump-autoload --optimize --no-dev
# With APCu cache (fastest):
composer dump-autoload --apcu --optimize --no-devLaravel Full Optimization
php artisan optimize
# Caches config + routes + views + autoloaderOptimization result: instead of filesystem lookups for each class, Composer uses an in-memory array lookup — 20–40% faster for large applications.