Req #51875 [Opn->Sus]: Magic __compare() method for objects

From: Date: Fri, 16 Mar 2018 23:32:05 +0000
Subject: Req #51875 [Opn->Sus]: Magic __compare() method for objects
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-214384@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=51875&edit=1 ID: 51875 Updated by: cmb@php.net Reported by: dennis dot birkholz at nexxes dot net Summary: Magic __compare() method for objects -Status: Open +Status: Suspended Type: Feature/Change Request Package: Class/Object related PHP Version: Irrelevant Block user comment: N Private report: N New Comment: > 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> Previous Comments: ------------------------------------------------------------------------ [2010-11-16 00:13:00] eric dot druid at gmail dot com This is a duplicate of http://bugs.php.net/bug.php?id=25772 ------------------------------------------------------------------------ [2010-05-20 22:06:41] 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 ------------------------------------------------------------------------ -- Edit this bug report at https://bugs.php.net/bug.php?id=51875&edit=1

« previous php.bugs (#214384) next »