spl_object_hash coincide, los objetos no son idénticos

I have two object variables in PHP, let's call them $a y $b. I assume they're both the same object. And, indeed, a call to spl_object_hash() confirms this, but they each have different properties.

Cuando corro:

if(spl_object_hash($a) === spl_object_hash($b)){
    echo "SAME HASH\n";
}else{
    echo "DIFFERENT HASH\n";
}

if(print_r($a,TRUE) === print_r($b,TRUE)){
    echo "SAME PRINT_R\n";
}else{
    echo "DIFFERENT PRINT_R\n";
}

if($a === $b){
        echo "IDENTICAL";
    }else{
        echo "NOT IDENTICAL";
    }

Yo obtengo:

SAME HASH
DIFFERENT PRINT_R
NOT IDENTICAL

This has got me baffled. When is the same object actually two different objects?

preguntado el 08 de noviembre de 11 a las 16:11

Este es el var_dump of both hashes. string(32) "0000000020d6b2fc000000007ad31222" string(32) "0000000020d6b2f0000000007ad31222" -

I wonder as well why the hashes are identical. Is there anything that makes these objects special? Can you add the definition of the objects in question and their states? -

And where did those variables come from? Can we reproduce it, or it would be too complicated? -

Am I crazy? Those hashes don't look the same to me. In php, === evaluates to false for me when I compare the two strings. -

They /are/ not the same: look in the middle: d6b2fc vs d6b2f0. -

2 Respuestas

There is a difference between being the same object, and having the same properties.

$a = new stdClass("same", "parameters", 1337);
$b = new stdClass("same", "parameters", 1337);

var_dump($a == $b); //True
var_dump($a === $b); //False!

$b = $a;

var_dump($a === $b); //Now true.

Respondido 28 Jul 12, 02:07

Depending on the PHP version and the operating system, PHP might cast the two compared hash strings to integers before comparing them (because they look numeric). Either because the resulting numbers are very large, or contain letters, the casting could result in data loss and thus lead to the same int value for both strings. Try this:

if ('X'.spl_object_hash($a) === 'X'.spl_object_hash($b)) { ...

Respondido 30 Abr '13, 17:04

Your argument is invalid with strict comparison. - Dejan Marjanović

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.