When writing compare functions, I often end up with a snippet like:
This is lots of code for intending to basically express:
If a is greater than b, return 1.
If a is smaller than b, return -1.
if a is equal to b, return 0.
If a is not comparable to b, for example when comparing “1” with 1, return null.
In Ruby there is the spaceship operator
<=>, serving exactly this purpose.
To learn how to build a composer package, I also came up with a PHP implementation of the spaceship operator (in the form of just a simple class), which can be found on Github.