PHP Semantic Versioning Helper

PHP Semantic Versioning Helper

SemVer is a semantic versioning helper library for PHP by Chris Kankiewicz with support for parsing, incrementing, and comparison.

Here’s the basic usage which initializes a SemVer\Version object:

use PHLAK\SemVer;

$version = new SemVer\Version(); // Initilializes to '0.1.0'

With an instance of SemVer\Version you can access individual values:

$version = new SemVer\Version('v1.2.3-beta.4+007');

echo $version;             // '1.2.3-beta.4+007'
echo $version->major;      // 1
echo $version->minor;      // 2
echo $version->patch;      // 3
echo $version->preRelease; // 'beta.4'
echo $version->build;      // '007'

If you wanted to build something that increments your package builds you can use this package’s increment methods:

$version = new SemVer\Version('v1.2.3');

$version->incrementMajor(); // v1.2.3 -> v2.0.0
$version->incrementMinor(); // v1.2.3 -> v1.3.0
$version->incrementPatch(); // v1.2.3 -> v1.2.4

Last, the comparisons are pretty handy:

$version1 = new SemVer('v1.2.3');
$version2 = new SemVer('v3.2.1');

$version1->gt($version2);  // false
$version1->lt($version2);  // true
$version1->eq($version2);  // false
$version1->neq($version2); // true
$version1->gte($version2); // false
$version1->lte($version2); // true

Before checking out this PHLAK/SemVer, I wasn’t aware that composer also has a composer/semver library, which has a comparator and semver class, however, I didn’t find the increment methods found in PHLAK/SemVer.

To get installation instructions, check out PHLAK/SemVer on GitHub!

Filed in: Laravel Packages


Join the weekly newsletter and never miss out on new tips, tutorials, and more.

Laravel News Partners

Laravel Jobs

PHP Web Developer
Orlando, FL
Christ for all Nations
PHP Developer
Manhattan, KS (open to Remote)
Mid-Senior Laravel Developer
Optix Solutions
Full Stack Web Developer
Las Vegas, NV
V Shred
Staff Software Engineer (Laravel)
Software Engineer (PHP, Laravel, Vue)
Los Angeles, CA
Software Engineer Level 1 / Level 2
St. Pete. / Tampa Area, FL / Remote