Skip to content

PHP Tutorial Composer

Managing Dependencies

Lesson 255 of 369 2 min read By Deepak Paswan Updated Oct 5, 2026

A big part of using Composer is deciding which versions you accept. Most PHP packages follow semantic versioning (semver):

Text
PLAIN
MAJOR.MINOR.PATCH     for example  2.10.3
PartChanges whenSafe to upgrade?
PATCH (2.10.3)Bug fixesYes
MINOR (2.10.3)New features, still compatibleYes
MAJOR (2.10.3)Breaking changesRead the upgrade guide

Version constraints

ConstraintMeansAccepts
2.10.3Exactly this version2.10.3 only
^2.10The caret: compatible with 2.102.10.0 up to, not including, 3.0.0
~2.10The tilde: this minor line2.10.0 up to, not including, 3.0.0
~2.10.32.10.3 up to, not including, 2.11.0
2.*WildcardAny 2.x version
>=2.0 <3.0A rangeSame as ^2.0
dev-mainA branchThe latest code on that branch (avoid in apps)

^ is what Composer writes for you with composer require. It is the right default: you get fixes and new features, but not breaking changes.

A tiny constraint checker

This function shows what ^2.10 means.

index.php
PHP
<?php
function matchesCaret(string $version, string $constraint): bool
{
    [$major, $minor] = array_map("intval", explode(".", ltrim($constraint, "^")));
    [$vMajor, $vMinor] = array_map("intval", explode(".", $version));

    return $vMajor === $major && $vMinor >= $minor;
}

var_dump(matchesCaret("2.10.3", "^2.10"));
var_dump(matchesCaret("2.9.0", "^2.10"));
var_dump(matchesCaret("3.0.0", "^2.10"));
Output
OUTPUT
bool(true)
bool(false)
bool(false)

Why constraints matter

  • Too loose (*): an update can break your app.
  • Too strict (2.10.3): you never get security fixes.
  • ^ is the sweet spot.

require vs require-dev

  • require: needed in production.
  • require-dev: tests, linters, debug bars. Not installed with --no-dev.

Platform requirements

data.json
JSON
{
    "require": {
        "php": "^8.2",
        "ext-json": "*",
        "ext-mbstring": "*"
    }
}

Composer checks that your PHP version and extensions match. The config.platform setting can pretend you have a certain PHP version, which is handy when you develop on a newer PHP than your production server runs.

Inspect your dependencies

terminal
BASH
composer show            # all installed packages
composer show --tree     # the dependency tree
composer why vendor/pkg  # why a package is installed
composer outdated        # what could be updated
composer audit           # known security problems

Keeping dependencies healthy

  1. Install only what you need. Every package is code you must trust and maintain.
  2. Prefer maintained packages. Check release dates and open issues.
  3. Update regularly, in small steps.
  4. Read changelogs for major versions.
  5. Run tests after every update.
  6. Run composer audit in your build.

Licences

Each package has a licence (MIT, GPL and so on). For commercial projects, make sure the licences fit how you use the code. composer licenses lists them.

dangerous
A package is code you run
Typosquatting (packages with names close to popular ones) and abandoned packages are real risks. Copy package names from the official documentation, not from a random post.