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?
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.
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.
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.
In simple words, == checks if both objects point to the same memory location whereas . equals() evaluates to the comparison of values in the objects.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With