For the complete documentation index, see llms.txt. This page is also available as Markdown.

Version

The Version component allows you an easy way to parse and compare versions. It supports both Semversion and package manager syntax.

Installation

composer require sonsofphp/version

Usage

The base Version supports semver standards.

<?php
use SonsOfPHP\Component\Version\Version;

// You can create versions two different ways
$version = new Version('1.2.3');
$version = Version::from('1.2.3');

echo $version; // prints "1.2.3"
$versionString = $version->toString(); // $versionString === "1.2.3"

// Easy API
$major = $version->getMajor(); // $major === 1
$minor = $version->getMinor(); // $minor === 2
$patch = $version->getPatch(); // $patch === 3

// Comparing Versions is easy too
$currentVersion = new Version('1.1.1');
$latestVersion  = new Version('1.1.2');

// if $latestVersion > $currentVersion
if ($latestVersion->isGreaterThan($currentVersion)) {
    // run upgrade
}
// Also supports "isLessThan" and "isEqualTo"

Advanced Usage

You can also use Pre-release and Build Metadata with your versions.

Last updated

Was this helpful?