|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #51875 Magic __compare() method for objects
Submitted: 2010-05-20 22:06 UTC Modified: 2018-03-16 23:32 UTC
Avg. Score:3.2 ± 0.7
Reproduced:1 of 1 (100.0%)
Same Version:0 (0.0%)
Same OS:0 (0.0%)
From: dennis dot birkholz at nexxes dot net Assigned:
Status: Suspended Package: Class/Object related
PHP Version: Irrelevant OS:
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If this is not your bug, you can add a comment by following this link.
If this is your bug, but you forgot your password, you can retrieve your password here.
Bug Type:
From: dennis dot birkholz at nexxes dot net
New email:
PHP Version: OS:


 [2010-05-20 22:06 UTC] dennis dot birkholz at nexxes dot net
Objects can either be compared by checking if they are equivalent (are references to the same object) or have the same values, which may lead to severe performance penalties or recursion errors while checking. (See

When using database stored objects, the identifier for the object is often stored as a single value within the object and it would be sufficient to compare the IDs of two different objects to know if they represent the same object in the database. In this cases, a === comparision would fail, the == comparison would succeed but could impose large overhead if a lot of other objects are referenced within the object. Sometimes even the == would fail because some caches are filled in one instance but not in the other.

There are several thinkable possibilities to solve this problem:
1. Use a magic method __compare() that returns a token for the object which then is used for comparison
2. Use a magic method __compare($obj) that returns true if the supplied object matches the "questioned" object
3. Use a magic method __compare() that returns an array containing a list of object properties to take into consideration, similar to the __sleep() magic method.

Test script:
class A {
	protected $ID;
	public function __construct($id) { $this->ID = $id; }
	// Variant 1
	public function __compare() { return $this->ID; }
	// Variant 2
	public function __compare($obj) { return ($this->ID == $obj->ID); }
	// Variant 3
	public function __compare() { return array('ID'); }

$a1 = new A(10);
$a2 = new A(20);
$a3 = new A(10);

echo ($a1 === $a2 ? "YES" : "NO") . "\n";
echo ($a1 === $a3 ? "YES" : "NO") . "\n";

Expected result:


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2010-11-16 00:13 UTC] eric dot druid at gmail dot com
This is a duplicate of
 [2018-03-16 23:32 UTC]
-Status: Open +Status: Suspended
 [2018-03-16 23:32 UTC]
> This is a duplicate of

Indeed, a dupe of bug #25772.  However, that bug report has been
closed as WONTFIX in the meantime, and even though I agree that
"those things are going to be to complicated and confusing", I
think pointing to the RFC process[1] is more appropriate nowadays.
Anybody is welcome to start it!  For the time being, I'm
suspending this ticket.

[1] <>
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri May 24 01:01:31 2024 UTC