From: Operating system: PHP version: Irrelevant Package: Class/Object related Bug Type: Feature/Change Request Bug description:Magic __compare() method for objects
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 bug report at http://bugs.php.net/bug.php?id=51875&edit=1 -- Try a snapshot (PHP 5.2): http://bugs.php.net/fix.php?id=51875&r=trysnapshot52 Try a snapshot (PHP 5.3): http://bugs.php.net/fix.php?id=51875&r=trysnapshot53 Try a snapshot (PHP 6.0): http://bugs.php.net/fix.php?id=51875&r=trysnapshot60 Fixed in SVN: http://bugs.php.net/fix.php?id=51875&r=fixed Fixed in SVN and need be documented: http://bugs.php.net/fix.php?id=51875&r=needdocs Fixed in release: http://bugs.php.net/fix.php?id=51875&r=alreadyfixed Need backtrace: http://bugs.php.net/fix.php?id=51875&r=needtrace Need Reproduce Script: http://bugs.php.net/fix.php?id=51875&r=needscript Try newer version: http://bugs.php.net/fix.php?id=51875&r=oldversion Not developer issue: http://bugs.php.net/fix.php?id=51875&r=support Expected behavior: http://bugs.php.net/fix.php?id=51875&r=notwrong Not enough info: http://bugs.php.net/fix.php?id=51875&r=notenoughinfo Submitted twice: http://bugs.php.net/fix.php?id=51875&r=submittedtwice register_globals: http://bugs.php.net/fix.php?id=51875&r=globals PHP 4 support discontinued: http://bugs.php.net/fix.php?id=51875&r=php4 Daylight Savings: http://bugs.php.net/fix.php?id=51875&r=dst IIS Stability: http://bugs.php.net/fix.php?id=51875&r=isapi Install GNU Sed: http://bugs.php.net/fix.php?id=51875&r=gnused Floating point limitations: http://bugs.php.net/fix.php?id=51875&r=float No Zend Extensions: http://bugs.php.net/fix.php?id=51875&r=nozend MySQL Configuration Error: http://bugs.php.net/fix.php?id=51875&r=mysqlcfg
