PHPackages                             jimbo2150/php-comparable - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. jimbo2150/php-comparable

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

jimbo2150/php-comparable
========================

PHP library to allow for comparison of two objects with a comparison operator.

v2.0.4(1y ago)18Apache-2.0PHPPHP &gt;=8.1

Since Feb 16Pushed 1y ago1 watchersCompare

[ Source](https://github.com/jimbo2150/php-comparable)[ Packagist](https://packagist.org/packages/jimbo2150/php-comparable)[ RSS](/packages/jimbo2150-php-comparable/feed)WikiDiscussions master Synced today

READMEChangelog (9)Dependencies (3)Versions (12)Used By (0)

[![Version](https://camo.githubusercontent.com/a3d64d4a1419066ef5e482767c680d2f2bf735b78422d3bf7f617b8805778e8e/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f762f72656c656173652f6a696d626f323135302f7068702d636f6d70617261626c65)](https://camo.githubusercontent.com/a3d64d4a1419066ef5e482767c680d2f2bf735b78422d3bf7f617b8805778e8e/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f762f72656c656173652f6a696d626f323135302f7068702d636f6d70617261626c65)[![License](https://camo.githubusercontent.com/7921495d32914ecf725b8c739feab2e8be088e978f906a964807a5545ac1de4d/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6c6963656e73652f6a696d626f323135302f7068702d636f6d70617261626c65)](https://camo.githubusercontent.com/7921495d32914ecf725b8c739feab2e8be088e978f906a964807a5545ac1de4d/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6c6963656e73652f6a696d626f323135302f7068702d636f6d70617261626c65)[![PHP Required Version](https://camo.githubusercontent.com/92d78469885c256210ae8bd1601d130aacc26eb041c1da9388bfc439ca39b77c/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f646570656e64656e63792d762f6a696d626f323135302f7068702d636f6d70617261626c652f706870)](https://camo.githubusercontent.com/92d78469885c256210ae8bd1601d130aacc26eb041c1da9388bfc439ca39b77c/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f646570656e64656e63792d762f6a696d626f323135302f7068702d636f6d70617261626c652f706870)

PHP Comparable Objects Trait &amp; Custom Comparison Convenience Method
=======================================================================

[](#php-comparable-objects-trait--custom-comparison-convenience-method)

A PHP library to allow for comparison of two objects with a comparison operator.

This library allows you to compare two objects using a value within the objects rather than the entirety of the object (the default for PHP).

Installation
------------

[](#installation)

This library is available on Packagist using Composer. Run this command within your composer project directory:

```
$ composer require jimbo2150/php-comparable
```

Usage (private value with convenience trait)
--------------------------------------------

[](#usage-private-value-with-convenience-trait)

The comparable trait (`ComparableTrait`) requires a protected method, `getComparableValue()`, which does not publically expose the value to be compared. The convenience methods then use reflection to access the other object's value:

```
use Jimbo2150\PhpComparable\Trait\ComparableTrait;

// This class will compare the Person object's $age property.
class Person
{
	use ComparableTrait;

	public function __construct(private string $name, private int $age)
	{
		assert($age > 0);
	}

	public function getComparableValue(): mixed
	{
		return $this->age;
	}
}

$john = new Person('John', 29);
$karen = new Person('Karen', 24);

$john->compareTo($karen); // False, compares by equals (==) by default
$karen->compareTo($john, Operator::from(' $operator->compare(
			$leftValue - $rightValue,
			self::MIN_REQUIRED_SCORE
		);

		return $this->customCompareTo(
			$diffFunction,
			$other,
			$operator
		);
	}

	public function getComparableValue(): mixed
	{
		return $this->score;
	}
}

$score1 = new Score(2.4);
$score2 = new Score(0.7);
$score3 = new Score(0.2);

$score1->compareDiff($score2); // True, score is >= 1
$score2->compareDiff($score3); // False, score is less than 1
```

###  Health Score

30

—

LowBetter than 62% of packages

Maintenance44

Moderate activity, may be stable

Popularity6

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity53

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~1 days

Total

11

Last Release

489d ago

Major Versions

v1.x-dev → v2.x-dev2025-02-23

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/25777094?v=4)[Jim Robinson](/maintainers/jimbo2150)[@jimbo2150](https://github.com/jimbo2150)

---

Top Contributors

[![jimbo2150](https://avatars.githubusercontent.com/u/25777094?v=4)](https://github.com/jimbo2150 "jimbo2150 (42 commits)")

---

Tags

comparecomparisonlibraryobject-comparisonoperatoroperatorsphpphp8traitutility

###  Code Quality

TestsPHPUnit

Code StylePHP CS Fixer

### Embed Badge

![Health badge](/badges/jimbo2150-php-comparable/health.svg)

```
[![Health](https://phpackages.com/badges/jimbo2150-php-comparable/health.svg)](https://phpackages.com/packages/jimbo2150-php-comparable)
```

###  Alternatives

[endclothing/prometheus_client_php

Prometheus instrumentation library for PHP applications.

1511.9M10](/packages/endclothing-prometheus-client-php)[tuupola/base58

Base58 encoder and decoder for arbitrary data

56672.8k26](/packages/tuupola-base58)[mvenghaus/filament-plugin-schedule-monitor

Filament Plugin for Spatie's Schedule Monitor

1556.5k1](/packages/mvenghaus-filament-plugin-schedule-monitor)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
