Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why this two objects are equal (==)?

In PHP I have two objects, they are different because of the $_frets variable (x is a string "x"), but PHP says

($o1 == $o2) == TRUE.

Why?

Dump of $o1:

guitarChord Object
(
    [_guitarChord:guitarChord:private] => 
    [_chord:guitarChord:private] => chord Object()
    [_baseFret:guitarChord:private] => 0
    [_frets:guitarChord:private] => Array
        (
            [0] => x
            [1] => 0
            [2] => 2
            [3] => 2
            [4] => 2
            [5] => x
        )

    [_tuning:guitarChord:private] => tuning Object()
    [currVariation] => 0
    [nextVariation] => 
    [prevVariation] => 
)

Dump of $o2:

guitarChord Object
(
    [_guitarChord:guitarChord:private] => 
    [_chord:guitarChord:private] => chord Object()
    [_baseFret:guitarChord:private] => 0
    [_frets:guitarChord:private] => Array
        (
            [0] => x
            [1] => 0
            [2] => 2
            [3] => 2
            [4] => 2
            [5] => 0
        )

    [_tuning:guitarChord:private] => tuning Object()
    [currVariation] => 0
    [nextVariation] => 
    [prevVariation] => 
)

EDIT:

So the reason why is because ("x" == 0) = TRUE. Can anyone tell me why?

like image 495
carlosdubusm Avatar asked May 20 '11 23:05

carlosdubusm


People also ask

How do you know if two objects are equal?

Whereas the equals() method compares two objects. Objects are equal when they have the same state (usually comparing variables). Objects are identical when they share the class identity. For example, the expression obj1==obj2 tests the identity, not equality.

How does object equal work?

In the first comparison, equals() compares the current object instance with the object that has been passed. If the two objects have the same values, equals() will return true . In the second comparison, equals() checks to see whether the passed object is null, or if it's typed as a different class.

Why are two identical objects not equal to each other?

Objects are not compared by value: two objects are not equal even if they have the same properties and values. This is true of arrays too: even if they have the same values in the same order. Objects are sometimes called reference types to distinguish them from JavaScript's primitive types.

What is the difference between using == and .equals on an object?

In simple words, == checks if both objects point to the same memory location whereas . equals() evaluates to the comparison of values in the objects.


1 Answers

Does x represent a string or does x represent null? If it represents null, then this is my theory: the Comparison Operators page has a transcription of the array comparison algorithm in Example #1. Based on this, I would imagine that in your case, what would end up happening is a comparison between 0 and null. According to the table above that, when null is compared to anything, it is converted to a bool. So you end up comparing 0 (false) to null (false), resulting in the two arrays being considered equal.

like image 168
ianlh Avatar answered Oct 14 '22 17:10

ianlh