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.
composer require monolog/monolog
require __DIR__ . '/vendor/autoload.php';
Try it
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.
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.
{ "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.lockfor applications so every machine and server gets identical versions. Libraries usually do not commit it. Never commitvendor/. installvsupdate:installobeys the lock file;updatechanges it. Runupdatedeliberately, review the diff, then commit.- Constraints.
^1.2allows1.xreleases from 1.2 up;~1.2.3allows only patch releases; an exact version pins it. Prefer^. - Add new namespaces to
autoload, then runcomposer 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.phpandsrc/models/user.phpare different files, even if your Windows or macOS machine tolerates it. platformcheck: thephpandext-*entries inrequirestop installs on servers that lack them.- Security: only install packages you trust, run
composer audit, and avoid running Composer as root. - Scripts: the
scriptssection runs commands after install or update, or on demand withcomposer run name.