azjezz/psl
PSL is a modern, well-typed standard library for PHP 8.4+, inspired by HHVM’s HSL. It offers safer, predictable APIs for async, collections, networking, I/O, crypto, terminal UI, and robust data validation—replacing brittle built-ins with consistent alternatives.
The Comparison component provides interfaces and functions for comparing values in a type-safe, consistent way. Instead of relying on PHP's loose comparison rules or scattering <=> operators throughout your code, you implement a Comparable interface and get a full suite of comparison operations for free.
Comparable<T> -- Interface for types that can be ordered relative to each otherEquable<T> -- Interface for types that can be checked for equalityOrder -- Enum with three cases: Less, Equal, Greatercompare(), equal(), less(), greater(), and moreOrder replaces magic integers (-1, 0, 1) with readable, type-safe cases:
@example('types/comparison-order.php')
The compare() function works with any values. For types implementing Comparable, it delegates to their compare() method. For everything else, it falls back to PHP's <=> operator:
@example('types/comparison-values.php')
Make your classes sortable and comparable by implementing the Comparable interface:
@example('types/comparison-comparable.php')
The sort() function returns an int suitable for PHP's usort() or PSL's Vec\sort:
@example('types/comparison-sorting.php')
All functions work with both Comparable types and plain scalars:
| Function | Description |
|---|---|
compare($a, $b) |
Returns Order enum |
equal($a, $b) |
True if values are equal |
not_equal($a, $b) |
True if values differ |
less($a, $b) |
True if $a < $b |
greater($a, $b) |
True if $a > $b |
less_or_equal($a, $b) |
True if $a <= $b |
greater_or_equal($a, $b) |
True if $a >= $b |
sort($a, $b) |
Returns int for use as a sort callback |
See src/Psl/Comparison/ for the full API.
How can I help you explore Laravel packages today?