Semantic Versioning and Version Constraints

5.Semantic Versioning and Version Constraints

M

In this chapter

We'll learn what the three numbers in a version like 2.4.1 promise, how Composer's version constraints work — exact, ranges, wildcards, ^ and ~ — why ~1.2 means something different in Composer than in npm, how 0.x versions are special, and what dev versions and stability flags are.

14–16 min

The Problem in Real Life

The old side project's composer.json says "textflow/sms": "*" — "any version". composer update happily installed TextFlow 3.0.0, released on Monday. Version 3 renamed send() to dispatch(), and all twelve calls in BookBay's code now fail.

TextFlow's changelog is clear about it: "3.0.0 — breaking: send() renamed to dispatch()". The version number itself said so too, for anyone who knew how to read it.

O

So the library warned us, and we told Composer to ignore the warning?

Olivia

"Any Version" vs. "Any Compatible Version"

Three numbers, three promises

MAJOR.MINOR.PATCH: only a new MAJOR number is allowed to break your code.

A range, not one version

A constraint like ^2.3 says which versions you accept, so updates bring fixes but not breaks.

"*" accepts everything

Including the next major version — the one that breaks your code.

Semantic Versioning and Version Constraints

Version numbers that mean something: Composer can only pick safe versions if version numbers follow rules, and if you tell it which versions you accept. This chapter covers:

  • Semantic versioning: MAJOR.MINOR.PATCH and what each promises.
  • Constraints: exact versions, ranges, wildcards, ^ and ~.
  • The 0.x rule: why ^0.3 is narrower than ^1.3.
  • Composer vs. npm: the same symbols, a different meaning for ~.
  • Stability: dev versions, @dev and minimum-stability.

Semantic versioning: almost every PHP package follows semantic versioning (semver): a version has three numbers, MAJOR.MINOR.PATCH, like 2.4.1. A new PATCH (2.4.1 → 2.4.2) only fixes bugs. A new MINOR (2.4.2 → 2.5.0) adds features but keeps everything that worked before working. A new MAJOR (2.5.0 → 3.0.0) is allowed to break things: rename methods, remove classes, change behaviour. So upgrading within the same major version should be safe, and a new major needs you to read the changelog and change code. TextFlow followed the rules: renaming send() meant 3.0.0. The diagram below shows which TextFlow versions each constraint accepts.

Which TextFlow versions does each constraint accept?

2.3.1

installed last month

2.4.0

security fix

2.4.1

bug fix

3.0.0

send() → dispatch()

accepts

"*"

all four — including 3.0.0, which breaks the code

accepts

"^2.3"

2.3.1, 2.4.0, 2.4.1 — every compatible version

accepts

"2.3.*"

2.3.1 only — misses the security fix

accepts

"2.3.1"

exactly 2.3.1 — never updated

Version constraints: in composer.json, each dependency gets a constraint: the set of versions you accept. The usual choice is the caret, ^2.3, meaning "2.3.0 or newer, but below 3.0.0" — every compatible version, nothing that may break. That's what composer require writes by default ("Using version ^9.0"). An exact version (2.3.1) never moves, so you'd miss security fixes. A wildcard (2.3.*) accepts patches only. Ranges with >=, <, a space for "and" and || for "or" (>=2.3 <2.6, ^2.3 || ^3.0) cover special cases. And * accepts everything — the cause of today's bug. The table below lists all of them.

Table — Composer version constraints
ConstraintMeansAccepts 2.4.1?Accepts 3.0.0?
2.3.1Exactly 2.3.1NoNo
2.3.*>=2.3.0 <2.4.0NoNo
^2.3>=2.3.0 <3.0.0 (recommended)YesNo
~2.3>=2.3.0 <3.0.0YesNo
~2.3.1>=2.3.1 <2.4.0NoNo
>=2.3 <2.6A range: space means andYesNo
^2.3 || ^3.0Either major: || means orYesYes
*Any version (avoid)YesYes
^0.3>=0.3.0 <0.4.0 (0.x is stricter)——

The tilde ~ and the npm trap: the tilde means "the last number you wrote may grow". In Composer, ~2.3 allows >=2.3.0 <3.0.0 (the same as ^2.3), but ~2.3.1 allows only >=2.3.1 <2.4.0 — patches. npm uses the same symbol differently: in npm, ~2.3 already means <2.4.0. If you come from JavaScript, read Composer's ~ carefully, or simply use ^, which means the same in both.

Table — Same symbol, different meaning
ConstraintComposernpm
^2.3>=2.3.0 <3.0.0>=2.3.0 <3.0.0
~2.3>=2.3.0 <3.0.0>=2.3.0 <2.4.0
~2.3.1>=2.3.1 <2.4.0>=2.3.1 <2.4.0

The 0.x rule: a package below version 1.0 is still "in development", and semver lets it break things in a minor release. So the caret is stricter there: ^0.3 means >=0.3.0 <0.4.0, and ^0.0.3 means exactly 0.0.3. If a library is still at 0.x, expect breaking changes in 0.4, 0.5 and so on, and read its changelog on every update.

Stability and dev versions: besides stable releases, a package can have pre-releases (3.0.0-beta1, 3.0.0-RC1) and dev versions taken straight from a Git branch (dev-main). By default Composer's minimum-stability is stable, so it never installs a beta or a branch unless you ask: per package with a flag, "textflow/sms": "^3.0@beta", or for the whole project with "minimum-stability": "dev" plus "prefer-stable": true (prefer stable releases whenever they exist). In production, depend on stable tagged versions: a branch can change under you between two installs.

Stability flags and project settings
{
"require": {
"textflow/sms": "^3.0@beta",
"bookbay/pricing": "dev-main"
},
"minimum-stability": "stable",
"prefer-stable": true
}

Fixing the side project: "textflow/sms": "^2.4" gets 2.4's security fix and every future 2.x release, and never 3.0. When BookBay is ready for 3.0, someone changes the constraint to ^3.0 on purpose, reads the changelog, renames send() to dispatch(), runs the tests and commits code and lock file together. composer outdated lists which packages have newer versions — the tilde ~ in its output marks a new major version that your constraint doesn't allow yet, an exclamation mark a minor or patch update you can take.

Finding updates: composer outdated
$ composer outdated --direct
Legend:
! patch or minor release available - update recommended
~ major release available - update possible
textflow/sms 2.4.1 ~ 3.0.0 SMS sending for PHP

Key Takeaway

Semantic versioning: PATCH fixes bugs, MINOR adds features compatibly, MAJOR may break code. In composer.json, use the caret: ^2.4 means >=2.4.0 <3.0.0, every compatible version and no breaking one; * accepts the next major too. Composer's ~2.3 equals ^2.3 but ~2.3.1 allows only patches — different from npm, where ~2.3 is patches only. Below 1.0 the caret narrows (^0.3 = >=0.3.0 <0.4.0). Composer installs only stable versions unless you allow beta or dev versions with @flags or minimum-stability; upgrade majors on purpose, with the changelog open.

Why This Matters

Version constraints decide what lands on your server the next time someone runs composer update. ^ gets you bug and security fixes automatically, and keeps breaking changes out until you're ready for them.

Semver is also a promise you make as soon as you publish a package (chapter 9): your colleagues' constraints only protect them if your version numbers tell the truth.

composer.json now says ^2.4, and the side project works again. Back in BookBay's project, Mia counts the require_once lines in bin/send-reminders.php: fourteen, one for every class file — and she forgot the new BookingStatus.php, so the script fails with "Class not found" on the server. Composer loads vendor packages without a single require. Can it do the same for BookBay's own classes? That's the next chapter: autoloading and PSR-4.