Giter Club home page Giter Club logo

comparable's Introduction

Comparable interface for PHP

Note: This is to the most part just code demonstrating the implementation of a magic interface for a tutorial. I do not currently plan on proposing including such an interface for PHP itself.

This extension implements a magic Comparable interface for PHP:

interface Comparable {
    static function compare($obj1, $obj2);
}

When two objects (both implementing the interface) are comapred using <, > or == the compare() method will be invoked. One should not rely on which class the method is invoked on. (Due to technical reasons for $l < $r it will be called on the class of $l, but for $l > $r it will be called on the class of $r.)

The compare() method can either return null to fall back to the default comparison behavior or one of the integer values -1 (for "smaller"), 0 (for "equal") and 1 (for "greater"). If the returned value is not an integer, it will be cast to one. If it is not one of -1, 0 or 1 it will be normalized to them.

An example (not sure how much sense it makes):

<?php

class Point implements Comparable {
    protected $x, $y, $z;

    public function __construct($x, $y, $z) {
        $this->x = $x; $this->y = $y; $this->z = $z;
    }

    public static function compare($p1, $p2) {
        if ($p1->x == $p2->x && $p1->y == $p2->y && $p1->z == $p2->z) {
            return 0;
        }

        if ($p1->x < $p2->x && $p1->y < $p2->y && $p1->z < $p2->z) {
            return -1;
        }

        if ($p1->x > $p2->x && $p1->y > $p2->y && $p1->z > $p2->z) {
            return 1;
        }

        return 1;
    }
}

$p1 = new Point(1, 1, 1);
$p2 = new Point(2, 2, 2);
$p3 = new Point(1, 0, 2);

var_dump($p1 < $p2, $p1 > $p2, $p1 == $p2); // true, false, false

var_dump($p1 == $p1); // true

var_dump($p1 < $p3, $p1 > $p3, $p1 == $p3); // false, false, false

Installation

The extension is installed as usual:

./configure --enable-comparable
make
make install

comparable's People

Contributors

nikic avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.