Object Comparison:
Equality: When comparing two object variables using the comparison operator (= =), the principle of comparison is:
1. If the properties and property values of the two objects are equal,
2. And two objects are instances of the same class (instance of), then the two object variables are equal.
Congruent: And if you use the strict equality operator (= = =), the two object variables must point to the same instance of a class (that is, the same object, which means that the object markers are exactly the same).
class sheep{ public $name; protected $food; public function __construct (string $name, string $food) { $this->name = $name; $this->food = $food; } public function __tostring () { return serialize ($this); } public Function __clone () { echo ' object is cloned ' . php_eol; }} $sheep 1 = new sheep (' radiant ', ' meat ');//PHP recoverable fatal error: object of class sheep could not be converted to stringecho $sheep 1;/** * * * Equality: When comparing two object variables with a comparison operator (= =), the principle of comparison is: if the properties and property values of two objects are equal, * and two objects are instances of the same class, the two object variables are equal. * * congruent: And if you use the congruent operator (= = =), these two object variables must point to the same instance of a class (that is, the same object). * */$sheep 1 = new sheep (' radiant ', ' meat '); $sheep 2 = new sheep (' Radiant '), ' meat '); $sheep 3 = clone $sheep 1;//Analysis: Three objects are instances of the sheep class, and the properties and values of the objects are exactly the same if ($sheep 1 == $sheep 2 && $sheep 2 == $sheep 3) { echo ' $ sheep1 == $sheep 2 == $sheep 3 ' . php_eol;} $sheep 4 = $sheep A reference to the 1; //object (passed an object identifier) if ($sheep 4 === $sheep 1) { echo ' $sheep 4 === $sheep 1 ' . php_eol;}
Comparison of PHP objects