0Pricing
PHP Academy · Lesson

Composer Scripts and Autoloading

Define scripts in composer.json and enable class autoloading.

Composer Scripts and Autoloading is a free PHP Academy lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the PHP Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Scripts in composer.json

The scripts key defines named shell commands that run via composer run-script NAME or just composer NAME.

Defining a Script

Scripts are arrays of shell commands or PHP static-method callbacks.

// composer.json
{
  "scripts": {
    "test": "vendor/bin/phpunit",
    "lint": "vendor/bin/phpcs src/",
    "cs-fix": "vendor/bin/phpcbf src/"
  }
}

Running Scripts

Execute a script with the shorthand composer followed by the name.

$ composer test
$ composer run-script lint

Event-Based Scripts

Composer fires lifecycle events you can hook into: post-install-cmd, post-update-cmd, post-autoload-dump.

// composer.json
{
  "scripts": {
    "post-install-cmd": ["php artisan migrate --force"]
  }
}

Chaining Scripts

Reference another script with @script-name inside a scripts array.

// composer.json
{
  "scripts": {
    "build": ["@lint", "@test"]
  }
}

Autoload Key Types

The autoload section supports three types: psr-4, classmap, and files.

// composer.json
{
  "autoload": {
    "psr-4":    { "App\\": "src/" },
    "classmap": ["database/"],
    "files":    ["src/helpers.php"]
  }
}

autoload-dev

autoload-dev adds autoload rules only for local/test environments — for test base classes that should not ship with the package.

Optimising the Autoloader

Run composer dump-autoload --optimize (or -o) to build a static classmap for faster class resolution. Ideal for production.

--classmap-authoritative

composer dump-autoload -a only looks in the classmap and never falls back to PSR-4 filesystem scanning. Use when all classes are mapped.

APCu Cache

composer dump-autoload --apcu uses APCu to cache class-to-file mappings in memory across requests — significant speedup on busy servers.

Practical CI Script Setup

Typical project scripts: test, cs (coding standards), analyse (PHPStan/Psalm), build (chain of all three).

Summary

Composer scripts automate repetitive tasks. The autoload section wires namespaces to files. Use dump-autoload -o in production for maximum performance.

Quick Check

Which event fires after "composer install" completes?

Frequently asked questions

Is the “Composer Scripts and Autoloading” lesson free?

Yes — the full text of “Composer Scripts and Autoloading” is free to read here on the web, and the PHP Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the PHP Academy course, upgrade to CoddyKit PRO.

What will I learn in “Composer Scripts and Autoloading”?

Define scripts in composer.json and enable class autoloading. You practise PHP Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start PHP Academy?

No prior experience is required. PHP Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Composer Scripts and Autoloading” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this PHP Academy lesson?

Yes. Every PHP Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Installing and Initializing Composer
  2. Requiring and Updating Packages
  3. Understanding composer.lock
  4. Composer Scripts and Autoloading
← Back to PHP Academy