Skip to content
Tutorials Center
My learning

Topics

PHP Reference ยท Web and Security

Composer for PHP

Reference for Composer: composer.json, require, install vs update, composer.lock, semantic version constraints, PSR-4 autoloading and production flags.

Composer is PHP's dependency manager. You list the packages your project needs in composer.json; Composer downloads them (from Packagist by default) into vendor/, resolves compatible versions, records the exact versions in composer.lock, and generates an autoloader so your classes and the libraries' classes load without a single require.

Composer is a command-line program, not a PHP function, and it needs network access and a writable project folder. The commands below are therefore read-only. The live examples show the autoloading idea that Composer automates, using plain PHP.

Terminal
composer require monolog/monolog
PHP
require __DIR__ . '/vendor/autoload.php';

Try it

PHP What a PSR-4 autoloader does

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with fgets(STDIN)), it gets the next line from this box.

PHP Reading and comparing version constraints

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with fgets(STDIN)), it gets the next line from this box.

composer.json

Read-only: a typical file.

JSON
{
    "name": "acme/shop",
    "require": {
        "php": "^8.4",
        "monolog/monolog": "^3.0"
    },
    "require-dev": {
        "pestphp/pest": "^3.0"
    },
    "autoload": {
        "psr-4": { "App\\": "src/" },
        "files": ["src/helpers.php"]
    },
    "config": {
        "optimize-autoloader": true
    }
}

Common commands

Command What it does
composer init Creates a composer.json interactively.
composer require vendor/package Adds a package, installs it and updates composer.lock. Add --dev for test tools.
composer install Installs the exact versions from composer.lock (or resolves them when there is no lock). Use this to deploy.
composer update [package] Re-resolves versions within the constraints and rewrites the lock file.
composer remove vendor/package Removes a package.
composer dump-autoload -o Regenerates the autoloader; -o builds an optimized class map.
composer install --no-dev --optimize-autoloader The production install.
composer audit Checks installed packages against known security advisories.
composer show, composer outdated Lists packages and available updates.

Usage notes

  • Commit composer.lock for applications so every machine and server gets identical versions. Libraries usually do not commit it. Never commit vendor/.
  • install vs update: install obeys the lock file; update changes it. Run update deliberately, review the diff, then commit.
  • Constraints. ^1.2 allows 1.x releases from 1.2 up; ~1.2.3 allows only patch releases; an exact version pins it. Prefer ^.
  • Add new namespaces to autoload, then run composer dump-autoload. A "class not found" error after adding a file is usually a missing dump or a file name that does not match the class name.
  • Case matters on Linux. src/Models/User.php and src/models/user.php are different files, even if your Windows or macOS machine tolerates it.
  • platform check: the php and ext-* entries in require stop installs on servers that lack them.
  • Security: only install packages you trust, run composer audit, and avoid running Composer as root.
  • Scripts: the scripts section runs commands after install or update, or on demand with composer run name.