GitXplorerGitXplorer
n

comparable

public
49 stars
5 forks
0 issues

Commits

List of commits on branch master.
Unverified
06193f429b127effdb6b586b9b2fc5e40b607c08

Always call compare() regardless of inheritance hierarchy

nnikic committed 12 years ago
Unverified
db48f57ae4f66b13e76e541f6d6606e1ed410a3e

Forgot to include output in readme

nnikic committed 12 years ago
Unverified
dde498ff760367c75cf760e90d2dcff4e3f04c04

Initial commit

nnikic committed 12 years ago

README

The README file for this repository.

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