note 73528 added to language.oop5.object-comparison

From: Date: Wed, 28 Feb 2007 04:27:26 +0000
Subject: note 73528 added to language.oop5.object-comparison
Groups: php.notes 
Request: Send a blank email to php-notes+get-123938@lists.php.net to get a copy of this message
I haven't found a build-in function to check whether two obects are identical - that is, all their fields are identical. In other words, <? class A { var $x; function __construct($x) { $this->x = $x; } } $identical1 = new A(42); $identical2 = new A(42); $different = new A('42'); ?> Comparing the objects with "==" will claim that all three of them are equal. Comparing with "===" will claim that all are un-equal. I have found no build-in function to check that the two identicals are identical, but not identical to the different. The following function does that: <? function deepCompare($a,$b) { if(is_object($a) && is_object($b)) { if(get_class($a)!=get_class($b)) return false; foreach($a as $key => $val) { if(!deepCompare($val,$b->$key)) return false; } return true; } else if(is_array($a) && is_array($b)) { while(!is_null(key($a) && !is_null(key($b)))) { if (key($a)!==key($b) || !deepCompare(current($a),current($b))) return false; next($a); next($b); } return true; } else return $a===$b; } ?> ---- Server IP: 82.103.133.188 Probable Submitter: 86.52.12.238 ---- Manual Page -- http://www.php.net/manual/en/language.oop5.object-comparison.php Edit -- https://master.php.net/note/edit/73528 Del: integrated -- https://master.php.net/note/delete/73528/integrated Del: useless -- https://master.php.net/note/delete/73528/useless Del: bad code -- https://master.php.net/note/delete/73528/bad+code Del: spam -- https://master.php.net/note/delete/73528/spam Del: non-english -- https://master.php.net/note/delete/73528/non-english Del: in docs -- https://master.php.net/note/delete/73528/in+docs Del: other reasons-- https://master.php.net/note/delete/73528 Reject -- https://master.php.net/note/reject/73528 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#123938) next »