Current Location: Home> Latest Articles> PHP Object Comparison Methods Explained: How to Use PHP Operators to Compare Objects

PHP Object Comparison Methods Explained: How to Use PHP Operators to Compare Objects

gitbox 2025-06-17

1. Introduction

When developing object-oriented PHP applications, comparing objects is a common operation, especially when determining if two objects are equal. This article delves into the methods and principles behind comparing objects in PHP.

2. Comparing Objects in PHP

In PHP, we can compare objects using comparison operators like ==, !=, ===, and !==. These operators help us determine if two objects are equal, have the same type, or contain the same values. Below is an explanation of each comparison operator:

  • ==: Compares if the property values of two objects are equal.
  • !=: Compares if the property values of two objects are not equal.
  • ===: Compares if two objects are the same (i.e., refer to the same memory address).
  • !==: Compares if two objects are not the same (i.e., refer to different memory addresses).

2.1 == Operator

The "==" operator compares the property values of two objects. If the values are equal, it returns true; otherwise, it returns false.

$obj1 = new stdClass();
$obj2 = new stdClass();
$obj1->name = 'Zhang San';
$obj1->age = 20;
$obj2->name = 'Zhang San';
$obj2->age = 20;

if ($obj1 == $obj2) {
    echo 'The two objects are equal';
} else {
    echo 'The two objects are not equal';
}

In the code above, since the property values of both objects are the same, it outputs "The two objects are equal".

2.2 != Operator

The "!=" operator checks if the property values of two objects are not equal. If they are different, it returns true; otherwise, it returns false.

$obj1 = new stdClass();
$obj2 = new stdClass();
$obj1->name = 'Zhang San';
$obj1->age = 20;
$obj2->name = 'Li Si';
$obj2->age = 30;

if ($obj1 != $obj2) {
    echo 'The property values of the two objects are not equal';
} else {
    echo 'The property values of the two objects are equal';
}

Since the property values of $obj1 and $obj2 are different, the code will output "The property values of the two objects are not equal".

2.3 === Operator

The "===" operator compares whether two objects are the same object, i.e., whether they refer to the same memory address.

$obj1 = new stdClass();
$obj2 = $obj1;

if ($obj1 === $obj2) {
    echo 'The two objects are the same object';
} else {
    echo 'The two objects are not the same object';
}

In this example, since $obj2 points to the same memory address as $obj1, it will output "The two objects are the same object".

2.4 !== Operator

The "!==" operator is the opposite of "===" and checks if two objects are not the same, i.e., they do not refer to the same memory address.

$obj1 = new stdClass();
$obj2 = $obj1;
$obj3 = new stdClass();

if ($obj1 !== $obj3) {
    echo 'The two objects are not the same object';
} else {
    echo 'The two objects are the same object';
}

Since $obj3 points to a different memory address than $obj1, it will output "The two objects are not the same object".

3. The Principle of Object Comparison

When PHP compares objects using the comparison operators, it automatically calls the __toString() method of the object to convert the object to a string before performing the comparison. If the object does not implement the __toString() method, PHP will throw a "Catchable fatal error".

Therefore, it is important to ensure that objects implement the __toString() method when performing object comparisons. Additionally, note that PHP compares the property values of the objects, not their methods. So, even if two objects have different methods, but their properties are the same, PHP will consider them equal.

4. Conclusion

This article covered the common methods and operators (==, !=, ===, !==) for comparing objects in PHP. When comparing objects, we should focus on the property values rather than the methods, and ensure that objects implement the __toString() method to avoid errors during comparison.