PHP Bugs  
php.net | support | documentation | report a bug | advanced search | search howto | statistics | login

go to bug id or search bugs for  

Bug #25772 Custom comparison of objects: __equals method
Submitted:7 Oct 2003 4:05am UTC Modified: 15 Apr 2004 3:29am UTC
From:dmitrijsl at swh-t dot lv Assigned to:
Status:Wont fix Category:Feature/Change Request
Version:5.0.0b1 (beta1) OS:*
Votes:7 Avg. Score:4.6 ± 0.7 Reproduced:6 of 6 (100.0%)
Same Version:1 (16.7%) Same OS:3 (50.0%)
View/Vote Developer Edit Submission

Have you experienced this issue?
Rate the importance of this bug to you:

[7 Oct 2003 4:05am UTC] dmitrijsl at swh-t dot lv
Description:
------------
If a class defines a __equals($other) function, invoke that function on
object comparison. Example:

==========================
class MyClass {
    public function __construct($value) {
        $this->value = $value;
    }

    /**
     * This function should be invoked on object
     * comparison with == or != operators.
     */
    public function __equals($other) {
        if ($other instanceof MyClass) {
            return ((int)$this->value
                   == (int)$other->value);
        } else {
            return false;
        }
    }

    private $value;
}

$a = new MyClass(3.14);
$b = new MyClass(3.13);

if ($a == $b) {
    echo '$a equals $b';
}
==========================

The comparison of $a and $b should result in the invokation of __equals
function of MyClass. The same should apply to the != (not equals)
operator.

[7 Oct 2003 5:21am UTC] helly@php.net
If at all then we should be able to do all comparisons directly by
__compare($other, $operator) where operator is one of {<,<=,==,!=,>,>=}
or by another method __less() which performs a < test so that __less()
and __equals() can be used to emulate all other tests.
[14 Apr 2004 10:58pm UTC] jevon at jevon dot org
Would love this feature! Java implements this through:

class Object {
   boolean equals(Object o);
}

So you could either make all user-defined objects include a simple
equals() function [and similarly, identical()].

Or perhaps, develop the aforementioned magic __equals() [and
__identical()] functions. (As well as __less())
[15 Apr 2004 3:29am UTC] derick@php.net
We're not going to have more magic then there already is; those things
are going to be to complicated and confusing (See the __string()) issue.

RSS feed | show source 

PHP Copyright © 2001-2009 The PHP Group
All rights reserved.
Last updated: Sat Nov 21 10:30:49 2009 UTC