From the documentation:
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, 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.
So, in answer to your question about in_array
, it appears that the two objects are compared using the comparison operator (==
). There is no conversion to string (via __toString()
or any other way) before comparison.
More generally, I have found this to be the case for almost all objects in PHP - the only exceptions are classes that encapsulate system resources, like PDO
or objects like closures (for reasons that I don't fully understand).
It is not necessary to cast the object(s) to strings before doing the comparison.