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

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
nadavvin
Forum Commoner
Posts: 68
Joined: Wed Sep 06, 2006 6:05 am

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

Post by nadavvin »

How do I know if two references point to the same instance?
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post 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;
}
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post 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";
User avatar
Maugrim_The_Reaper
DevNet Master
Posts: 2704
Joined: Tue Nov 02, 2004 5:43 am
Location: Ireland

Post 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.
nadavvin
Forum Commoner
Posts: 68
Joined: Wed Sep 06, 2006 6:05 am

Post 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 "==="?
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

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

Post 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
nadavvin
Forum Commoner
Posts: 68
Joined: Wed Sep 06, 2006 6:05 am

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

Post 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
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post 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) )
{

// ...
nadavvin
Forum Commoner
Posts: 68
Joined: Wed Sep 06, 2006 6:05 am

Post 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) )
{

}
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post 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.
Post Reply