How do I know if two references point to the same instance?
Posted: Tue Feb 27, 2007 5:04 am
How do I know if two references point to the same instance?
A community of PHP developers offering assistance, advice, discussion, and friendship.
http://forums.devnetwork.net/
Code: Select all
function is_reference(&$o1, &$o2)
{
$prop_base = "_is_reference_test_var_";
$i = 1;
$prop = "";
do
{
$prop = $prop_base . $i;
$i++;
}
while (isset($o1->$prop) || isset($o2->$prop));
//Now we know $prop DOES NOT exist in $o1 or $o2
$o1->$prop = true;
$is_reference = isset($o2->$prop);
unset($o1->$prop);
return $is_reference;
}Code: Select all
$o1 = new stdClass;
$o2 = $o1;
$o3 = new stdClass;
echo '$o1===$o2 ', $o1===$o2 ? 'true':'false', "<br />\n";
echo '$o1===$o3 ', $o1===$o3 ? 'true':'false', "<br />\n";So I need to use "==" or "==="?Maugrim_The_Reaper wrote:Can also note simple comparison with == (not the identical comparator ===) can create recursion errors in PHP5.2. That caused a nasty mess in SimpleTest until it was patched.
===nadavvin wrote:How do I know if two references point to the same instance?
thanksvolka wrote:===nadavvin wrote:How do I know if two references point to the same instance?
see http://de2.php.net/manual/en/language.o ... arison.php
Code: Select all
Two instances of the same class
o1 == o2 : TRUE
o1 != o2 : FALSE
o1 === o2 : FALSE
o1 !== o2 : TRUE
Two references to the same instance
o1 == o2 : TRUE
o1 != o2 : FALSE
o1 === o2 : TRUE
o1 !== o2 : FALSECode: Select all
if ( (get_object_vars($obj1) === get_object_vars($obj2))
&& (get_class($obj1) === get_class($obj2) )
{
// ...I think this do it:How would I compare two objects and depict if they are the same class, have the same properties, and the same data, but are not the same instance?
Code: Select all
if ( ($o1 == $o2) && !($o1===$o2) )
{
}