composer.json deep dive

A PHP project hands its dependencies to Composer, and everything Composer knows about the project comes from one file. The sections below read that file from top to bottom, and the last one runs the two commands that use it. The earlier lesson Composer and autoloading installs a package for the first time; this page is the reference for the file itself.

Sections tour

The composer file describes the package name, the PHP version it needs, and the libraries it depends on. Each top level key has one clear job.

Keep require strict, put dev tools in require dev, and review the whole file before sharing the project.

{ "name": "acme/demo", "description": "Demo project", "require": { "php": ">=8.1" }, "require-dev": { "phpunit/phpunit": "^10.0" } }

Scripts

Scripts give short names to long commands used every day. Team members then run the same checks with one memorable word.

Common scripts run tests, fix code style, and clear caches after deploy steps.

{ "scripts": { "test": "phpunit --colors=always", "lint": "php -l src/index.php", "start": "php -S localhost:8000 -t public" } }

PSR-4 map

The autoload section maps namespace prefixes to folders. Composer then loads class files on demand without manual includes.

Match folder names to namespace parts exactly, since a small typo breaks loading on case sensitive systems.

{ "autoload": { "psr-4": { "Acme\\Demo\\": "src/" } } }

Update safely

Update resolves fresh versions while install repeats the locked set. Run update on purpose, review the diff, and commit the lock file.

Back up the lock file first on large projects, so a bad update can be rolled back fast.

composer update

Lock file updated. Run composer install on other machines.

Exercises

  1. Add your own name as a script and run it with composer run.
  2. Move phpunit/phpunit from require-dev to require and explain what changes for a production install.
  3. Write a PSR-4 map for a namespace with two parts and say which folder a class of it would land in.
  4. Delete composer.lock in a copy of a project, run composer install and describe what Composer does instead.
  5. Explain why the lock file belongs in version control while the vendor folder does not.

Questions

A new set of questions in a new order every time. Choose one answer for each, then check.

  1. Which key names the PHP version a package needs?

    Answer

    require

    The require map holds the runtime dependencies, and php is one of its entries.

  2. What does this program print? <?php var_dump(version_compare("8.5.13", "8.1", ">="));

    Answer

    bool(true)

    The installed PHP version is greater than the minimum, so the requirement of the file is satisfied.

  3. What does this program print? <?php $scripts = ["test" => "phpunit --colors=always", "lint" => "php -l src/index.php"]; echo $scripts["lint"];

    Answer

    php -l src/index.php

    The scripts map is read by name, and the lint entry holds the command that checks one file.

  4. What does the autoload section with a psr-4 map describe?

    Answer

    Which namespace prefix lives in which folder

    The prefix is turned into a path, so the class file is required on first use.

  5. What does this composer.json require? { "require": { "php": ">=8.1" }, "require-dev": { "phpunit/phpunit": "^10.0" } }

    Answer

    PHP 8.1 or newer at run time and PHPUnit 10 only for development

    The require map is the runtime set, and require-dev is installed only when development packages are asked for.

  6. What does composer install read the versions from?

    Answer

    composer.lock when it exists

    install repeats the locked set, which is why the lock file is committed.

  7. What does composer update do that composer install does not?

    Answer

    It resolves fresh versions and rewrites the lock file

    update looks for newer versions that fit the constraints, and install only replays the lock.

  8. What does this program print? <?php $map = ["Acme\\Demo\\" => "src/"]; $class = "Acme\\Demo\\User"; foreach ($map as $prefix => $dir) { if (str_starts_with($class, $prefix)) { echo $dir . str_replace("\\", "/", substr($class, strlen($prefix))) . ".php"; } }

    Answer

    src/User.php

    The prefix is stripped from the class name and the rest of the name becomes the path inside src/.

  9. What does this program print? <?php $json = '{"require":{"php":">=8.1"},"require-dev":{"phpunit/phpunit":"^10.0"}}'; $data = json_decode($json, true); echo implode(",", array_keys($data)), PHP_EOL; echo $data["require-dev"]["phpunit/phpunit"], PHP_EOL;

    Answer

    require,require-dev / ^10.0

    json_decode() keeps the two top level keys, and the version constraint is the value of the dev entry.

  10. Why is the vendor folder usually kept out of version control?

    Answer

    Because it is rebuilt from the lock file by composer install

    The lock file pins every version, so the folder can be restored exactly without being committed.

Every composer.json block on this page parses as valid JSON: it was read with json.loads of Python 3.14.10. The line under composer update is a summary of what the command reports, not the log of a run: that log names every package it locks and is dozens of lines long.

Article author: Arthur Isaev