PHP code example of fleshgrinder / comparable

1. Go to this page and download the library: Download fleshgrinder/comparable library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

fleshgrinder / comparable example snippets


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;
    }

}