#25772 [Opn]: Custom comparison of objects: __equals method
| From: | helly@php.net | Date: | Tue, 07 Oct 2003 09:21:33 +0000 |
| Subject: | #25772 [Opn]: Custom comparison of objects: __equals method | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-48626@lists.php.net to get a copy of this message | ||
ID: 25772
Updated by: helly@php.net
Reported By: dmitrijsl at swh-t dot lv
Status: Open
Bug Type: Feature/Change Request
-Operating System: Windows 2000
+Operating System: *
PHP Version: 5.0.0b1 (beta1)
New Comment:
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.
Previous Comments:
------------------------------------------------------------------------
[2003-10-07 04:05:43] 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.
------------------------------------------------------------------------
--
Edit this bug report at http://bugs.php.net/?id=25772&edit=1