Table of Contents

PHP RFC: Comparator interface

Introduction

The internal DateTime and ImmutableDateTime classes have an internal compare_objects() method, which allows them to be directly compared with the usual comparison operators (<, <=, ==, > and >=). This is currently not possible to implement with user defined classes, which this RFC tries to solve.

Proposal

Following the example of similar interface which enhance the internal functionality of a user class, this RFC proposes to have an API interface, which the Zend engine can use to compare two objects of the same type:

interface Comparator
{
    public function compare($other) : int;
}

The value of the compare() method must be either smaller than 0 if the object the function is called on is smaller than $other, 0 if both are considered equal or else an integer larger than 0. Thus, when a user tries to compare two objects with the normal comparison operators, PHP will internally call the compare() method of the first operand and pass the second operand as method parameter.

Proposed PHP Version(s)

Poposed for the next PHP 7.x.

Patches and Tests

A pull request with an interface, targeting master, is here: https://github.com/php/php-src/pull/2143

Vote

As this is a language change, a 2/3 majority is required. A straight Yes/No vote is being held.

Rejected Features

Keep this updated with features that were discussed on the mail lists.