phar-io/version
phar-io/version is a PHP library for parsing, comparing, and validating semantic versions and version constraints. Supports operators like >=, <=, caret (^) and tilde (~) ranges, plus pre-release labels, to check if versions comply.
Install via composer require phar-io/version (or --dev for tooling). Begin with the two core classes: PharIo\Version\Version for parsing version strings (e.g., new Version('2.1.3-beta1')) and PharIo\Version\VersionConstraintParser for parsing constraint strings (e.g., ^8.1, ~3.2.0, >=7.4,<8.0). The most common first use case is enforcing PHP version requirements at application entry points — especially in PHARs or CLI tools — by checking compatibility with PHP_VERSION:
use PharIo\Version\Version;
use PharIo\Version\VersionConstraintParser;
$parser = new VersionConstraintParser();
$constraint = $parser->parse('^8.1');
if (! $constraint->complies(new Version(PHP_VERSION))) {
fwrite(STDERR, 'This tool requires PHP ^8.1'.PHP_EOL);
exit(1);
}
bootstrap/app.php or console command providers to fail fast if requirements aren’t met.composer.json extra.plugins), parse each plugin’s declared requirement (e.g., "require": {"php": "^8.0"}) using VersionConstraintParser, then verify against the host app’s version before autoloading.^8.0,<9.0) or manually combine via AndVersionConstraint to express complex policies — e.g., '^8.0, !*dev' to allow only stable 8.x releases.Version::hasPreReleaseSuffix() and isGreaterThan() to compare pre-release versions (e.g., 3.0.0-beta2 > 3.0.0-alpha3) and surface appropriate upgrade paths.equals() (added in v3.2.0) to compare versions with build metadata (e.g., 1.2.3+build.42), though remember it’s ignored for range constraints (^1.2.3 matches 1.2.3+build.any).Version + constraint objects — ideal for testing dependency version guards without mocking or external process calls.v Prefix: Parsed but stripped internally (new Version('v1.0.0') → '1.0.0'). Use getOriginalString() if you need to preserve it for logs/UI.3.0.0 > 3.0.0-rc1), and suffixes like beta, alpha, dev must be lowercase — uppercase fails in comparisons (fixed in v3.0.4, but old tests may break).~1.2 behaves identically to ^1.2 (i.e., <2.0.0), not <1.3.0. Only ~1.2.3 restricts to patch-level versions (<1.3.0).^6 (no minor/patch) are now supported (v3.3.0+) and expanded to ^6.0.0 internally.| is not supported. Logical OR must be represented via multiple top-level constraints — not via VersionConstraintParser directly. Workaround: parse constraints separately and implement OrVersionConstraint manually (or use Composer’s * wildcard and custom logic).Version::getVersionString() returns string, not null).How can I help you explore Laravel packages today?