Page 1 of 1

How do I know if two references point to the same instance?

Posted: Tue Feb 27, 2007 5:04 am
by nadavvin
How do I know if two references point to the same instance?

Posted: Tue Feb 27, 2007 5:13 am
by Chris Corbyn
Be naughty and inject something into one then see if it's there in the other.

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;
}

Posted: Tue Feb 27, 2007 5:35 am
by volka
In php5 you can use ===

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";

Posted: Tue Feb 27, 2007 5:48 am
by Maugrim_The_Reaper
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.

Posted: Tue Feb 27, 2007 6:33 am
by nadavvin
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.
So I need to use "==" or "==="?

Re: How do I know if two references point to the same instan

Posted: Tue Feb 27, 2007 6:38 am
by volka
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

Re: How do I know if two references point to the same instan

Posted: Tue Feb 27, 2007 6:47 am
by nadavvin
volka 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
thanks

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 : FALSE

Posted: Tue Feb 27, 2007 6:47 am
by Jenk
Here is a hypothetical question..

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?

Would it simply be:

Code: Select all

if ( (get_object_vars($obj1) === get_object_vars($obj2))
    && (get_class($obj1) === get_class($obj2) )
{

// ...

Posted: Tue Feb 27, 2007 6:50 am
by nadavvin
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?
I think this do it:

Code: Select all

if ( ($o1 == $o2) && !($o1===$o2) )
{

}

Posted: Tue Feb 27, 2007 7:00 am
by Chris Corbyn
Maugrim_The_Reaper already pointed out that == can cause infinite recursion. You'll get "Nesting level too deep" errors... don't do that.