php.net |  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
Votes:8
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
View Add Comment Developer Edit
Anyone can comment on a bug. Have a simpler test case? Does it work for you on a different platform? Let us know!
Just going to say 'Me too!'? Don't clutter the database with that please — but make sure to vote on the bug!
Your email address:
MUST BE VALID
Solve the problem:
22 + 25 = ?
Subscribe to this entry?

 
 [2010-05-20 22:06 UTC] dennis dot birkholz at nexxes dot net
Description:
------------
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 http://de.php.net/manual/en/language.oop5.object-comparison.php)

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:
----------------
NO
YES


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2010-11-16 00:13 UTC] eric dot druid at gmail dot com
This is a duplicate of http://bugs.php.net/bug.php?id=25772
 [2018-03-16 23:32 UTC] cmb@php.net
-Status: Open +Status: Suspended
 [2018-03-16 23:32 UTC] cmb@php.net
> This is a duplicate of http://bugs.php.net/bug.php?id=25772

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] <https://wiki.php.net/rfc/howto>
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Mar 28 20:01:28 2024 UTC