PHPackages                             fleshgrinder/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. fleshgrinder/comparable

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

fleshgrinder/comparable
=======================

Interface for custom compare checks of objects.

v0.1.0(10y ago)110.5k—9.8%1MITPHP

Since Sep 23Pushed 9y agoCompare

[ Source](https://github.com/Fleshgrinder/php-comparable)[ Packagist](https://packagist.org/packages/fleshgrinder/comparable)[ RSS](/packages/fleshgrinder-comparable/feed)WikiDiscussions master Synced 2w ago

READMEChangelogDependenciesVersions (4)Used By (1)

[![Packagist](https://camo.githubusercontent.com/6bbd1239e86d0fc165b2765a482cba519a8b4436db6561907cb6030a1bfb28ea/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f466c6573686772696e6465722f636f6d70617261626c652e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/fleshgrinder/comparable)[![Packagist License](https://camo.githubusercontent.com/e96b68eacc757bb136cfb9e06509e16f96750763b9d6296b18dc4c97ec668a4a/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f6c2f466c6573686772696e6465722f636f6d70617261626c652e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/fleshgrinder/comparable)

Comparable
==========

[](#comparable)

Interface to implement custom comparison logic for classes instead of writing them inline over and over again.

Provided is the interface itself that establishes the contract that implementing classes have to have the compare method. Furthermore a specific exception is provided that can be used to notify callers that a comparison of the passed value is not possible. Last but not least a dummy class that can be used in tests as a substitute for doubles, stubs, or mocks is included as well.

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

[](#installation)

Open a terminal, enter your project directory and execute the following command to add this package to your dependencies:

```
$ composer require fleshgrinder/comparable
```

This command requires you to have Composer installed globally, as explained in the [installation chapter](https://getcomposer.org/doc/00-intro.md) of the Composer documentation.

Usage
-----

[](#usage)

Simply implement the interface and the required `compareTo` method.

```
class YourClass implements Comparable {

    const TOLERANCE = 0.0001;

    protected $value;

    public function __construct($value) {
        $this->value = (float) $value;
    }

    /**
     * @inheritDoc
     */
    public function compareTo($other) {
        if (($other instanceof $this) === false) {
            throw new UncomparableException();
        }

        $diff = $other->value() - $this->value;

        if ($diff > static::TOLERANCE) {
            return 1;
        }

        if ($diff < -static::TOLERANCE) {
            return -1;
        }

        return 0;
    }

}
```

License
-------

[](#license)

[![MIT License](https://camo.githubusercontent.com/f8370bd5022685ccc4cdebd136a849a7692c5afd674e59ca8805d90f2aa29a72/68747470733a2f2f75706c6f61642e77696b696d656469612e6f72672f77696b6970656469612f636f6d6d6f6e732f7468756d622f632f63332f4c6963656e73655f69636f6e2d6d69742e7376672f343870782d4c6963656e73655f69636f6e2d6d69742e7376672e706e67)](https://opensource.org/licenses/MIT)

###  Health Score

31

—

LowBetter than 65% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity26

Limited adoption so far

Community8

Small or concentrated contributor base

Maturity55

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 ~3 days

Total

2

Last Release

3977d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/6680298143064cd3ac63c0097ef724d46f47221ae0990db445081c4e8a923e05?d=identicon)[Fleshgrinder](/maintainers/Fleshgrinder)

---

Top Contributors

[![Fleshgrinder](https://avatars.githubusercontent.com/u/1059453?v=4)](https://github.com/Fleshgrinder "Fleshgrinder (5 commits)")

---

Tags

comparisonphpphp7sortingtype-safety

### Embed Badge

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

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

###  Alternatives

[lara-zeus/quantity

filamentphp Input Number component, with user-friendly increment and decrement controls

1979.9k11](/packages/lara-zeus-quantity)

PHPackages © 2026

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