Skip to main content

Lesson 3 of 6

Composer - Packages, Scripts & Autoload Optimisation

Composer is PHP's package manager. Beyond installing packages, it runs scripts, publishes assets, validates lock files, and optimises the autoloader for production. Understanding how the autoloader works - and how to optimise it - directly impacts application startup time.

11 minBy Priygop Editorial TeamUpdated 2026Peer Reviewed & Verified

Composer Deep Dive - Scripts & Optimisation

Composer Deep Dive - Scripts & Optimisation
# ── composer.json - production-ready structure ─────────────────
# {
#   "name": "priygop/blog",
#   "description": "Production PHP Blog System",
#   "type": "project",
#   "license": "MIT",
#   "require": {
#     "php": "^8.2",
#     "ext-pdo": "*",
#     "ext-mbstring": "*",
#     "vlucas/phpdotenv": "^5.6",
#     "monolog/monolog": "^3.5",
#     "firebase/php-jwt": "^6.10"
#   },
#   "require-dev": {
#     "phpunit/phpunit": "^11.0",
#     "phpstan/phpstan": "^1.10",
#     "squizlabs/php_codesniffer": "^3.8"
#   },
#   "autoload": {
#     "psr-4": { "App\\": "src/" },
#     "files": ["src/helpers.php"]
#   },
#   "autoload-dev": {
#     "psr-4": { "App\\Tests\\": "tests/" }
#   },
#   "scripts": {
#     "post-install-cmd": ["@php artisan config:clear"],
#     "test":     "phpunit --coverage-html storage/coverage",
#     "analyse":  "phpstan analyse src tests --level=8",
#     "cs":       "phpcs src --standard=PSR12",
#     "cs-fix":   "phpcbf src --standard=PSR12",
#     "dev":      "php -S localhost:8000 -t public/"
#   },
#   "config": {
#     "optimize-autoloader": true,
#     "preferred-install": "dist",
#     "sort-packages": true
#   }
# }

# ── Key Composer commands ─────────────────────────────────────
# composer install                         Install from composer.lock
# composer install --no-dev                Skip dev packages (production)
# composer install --no-dev --optimize-autoloader  Production install

# --optimize-autoloader (same as -o) - converts PSR-4 to class map cache
# Each class lookup goes from "compute path from namespace" to "array key lookup"
# Significantly faster on cold starts with many classes

# composer dump-autoload --optimize        Regenerate optimised class map
# composer dump-autoload --classmap-authoritative  Fastest: no fallback scan

# ── Composer scripts ─────────────────────────────────────────
# Run like: composer test   composer analyse   composer dev
#           composer cs-fix

# ── Package security audit ────────────────────────────────────
# composer audit                Check for known vulnerabilities in packages

<?php
// ── Autoloader optimisation explained ────────────────────────
// Without optimisation (PSR-4 dynamic):
// require "UserService.php" → composer computes:
// "App\Blog\Service\UserService" → "src/Blog/Service/UserService.php"
// This computation (string manipulation) happens every class load.

// With --optimize-autoloader, Composer builds a class map:
// [
//   "App\Blog\Service\UserService" => "/var/www/html/src/Blog/Service/UserService.php",
//   "App\Blog\Domain\Post"          => "/var/www/html/src/Blog/Domain/Post.php",
//   ... (every class pre-mapped)
// ]
// Lookup is O(1) array access - no filesystem stat calls.

// ── helpers.php - auto-loaded globally (add to "files" in autoload) ──
function e(mixed $value): string {
    return htmlspecialchars((string) $value, ENT_QUOTES | ENT_SUBSTITUTE, "UTF-8");
}

function dd(mixed ...$vars): never {
    header("Content-Type: text/plain");
    foreach ($vars as $var) { var_dump($var); }
    exit;
}

function env(string $key, mixed $default = null): mixed {
    return $_ENV[$key] ?? $_SERVER[$key] ?? $default;
}

function config(string $key, mixed $default = null): mixed {
    static $config = null;
    if ($config === null) $config = require __DIR__ . "/../config/app.php";
    return $config[$key] ?? $default;
}

Quick Quiz

Tip

Tip

Practice Composer Packages Scripts Autoload Optimisation in small, isolated examples before integrating into larger projects. Breaking concepts into small experiments builds genuine understanding faster than reading alone.

Diagram
Loading diagram…

Visual architecture and concepts for Composer - Packages, Scripts & Autoload Optimisation

Practice Task

Note

Practice Task - (1) Write a working example of Composer Packages Scripts Autoload Optimisation from scratch without looking at notes. (2) Modify it to handle an edge case (empty input, null value, or error state). (3) Share your solution in the Priygop community for feedback.

Common Mistake

Warning

A common mistake with Composer Packages Scripts Autoload Optimisation is skipping edge case testing - empty inputs, null values, and unexpected data types. Always validate boundary conditions to write robust, production-ready php code.

Hands-on Practice Tools for this Course

Test what you learn using these free in-browser developer utilities (no signup required).

All 40+ Tools

Topics in This Module

0/6 done