When using the comparison operator [==
], object variables are compared in a simple manner, namely: Two object instances are equal if they have the same attributes and values [values are compared with ==
], and are instances of the same class.
When using the identity operator [===
], object variables are identical if and only if they refer to the same instance of the same class.
An example will clarify these rules.
Example #1 Example of object comparison
The above example will output:
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 Instances of two different classes o1 == o2 : FALSE o1 != o2 : TRUE o1 === o2 : FALSE o1 !== o2 : TRUE
Note:
Extensions can define own rules for their objects comparison [
==
].
jazfresh at hotmail.com ¶
15 years ago
Note that when comparing object attributes, the comparison is recursive [at least, it is with PHP 5.2]. That is, if $a->x contains an object then that will be compared with $b->x in the same manner. Be aware that this can lead to recursion errors:
Results in:
PHP Fatal error: Nesting level too deep - recursive dependency? in test.php on line 11
Anonymous ¶
12 years ago
Comparison using operators should be documented. Between two objects, at least in PHP5.3, the comparison operation stops and returns at the first unequal property found.
rnealxp at yahoo dot com ¶
2 years ago
Please use this corrected version of function "valuesAreIdentical" instead of that which I previously posted [dependencies found in previous post]; if an Admin can just replace the fn snippet, awesome/thanks, otherwise, apologies.
rnealxp at yahoo dot com ¶
5 years ago
These three functions call themselves recursively and handle any nesting levels of arrays/objects/values and do strict comparisons. The entry-point to this function set would be "valuesAreIdentical".
nhuhoai ¶
8 years ago
For comparison about two objects in a class, you can use an interface like this and customize your functions for each class:
If you gotcha a super class, you can make generic functions [not safe but work with not complex class]:
wbcarts at juno dot com ¶
14 years ago
COMPARING OBJECTS using PHP's usort[] method.
PHP and MySQL both provide ways to sort your data already, and it is a good idea to use that if possible. However, since this section is on comparing your own PHP objects [and that you may need to alter the sorting method in PHP], here is an example of how you can do that using PHP's "user-defined" sort method, usort[] and your own class compare[] methods.
The practical upshot is that using "==" when "===" would be more appropriate can result in a severe performance penalty, especially if the objects are large and/or complex. In fact, if there are any circular relationships involved between the objects or [recursively] any of their properties, then a fatal error can result because of the implied infinite loop.
Even though $link1 and $link2 contain different Leaf objects, they are still equivalent because the Leaf objects are themselves equivalent.
So preference should be given to comparing objects with "===" rather than "=="; if two distinct objects are to be compared for equivalence, try to do so by examining suitable individual properties. [Maybe PHP could get a magic "__equals" method that gets used to evaluate "=="? :] ]
Oddant ¶
9 years ago
This example is way too much confusing, if you new to php comparison motor, you should think [after reading this example] that '==' is actually comparing the type of the objects. that's not true, it actually compares the type of the objects AND the properties of them.