How to compare 2 objects in PHP

Updated: January 10, 2024 By: Guest Contributor Post a comment

Overview

Comparing objects in PHP can be a nuanced task, especially considering the difference between object references and the values the objects hold. This tutorial will guide you through various methods to effectively compare objects in PHP.

Checking Object Type and Attributes

Before diving into object comparison, ensure that the objects you are about to compare are indeed of the same type or class. Every object in PHP can be checked using the instanceof operator.


$object1 = new MyClass();
$object2 = new MyClass();

echo $object1 instanceof MyClass; // Output: 1 (true)
echo $object2 instanceof MyClass; // Output: 1 (true)

Now that we have confirmed the objects are of the same type, we can compare the objects in different ways.

Simple Comparison with ==

The simplest way to compare two objects in PHP is using the equality operator ==, which will check if the objects have the same attributes and values, but it doesn’t check if the two variables point to the same instance.


if($object1 == $object2) {
  echo 'The objects are the same in terms of attributes and values'; 
} else {
  echo 'The objects are not the same';
}

Exact Comparison with ===

For a more stringent comparison, we can use the identical operator ===. This confirms whether the variables literally refer to the same instance of the class.


if($object1 === $object2) {
  echo 'These are the same instances';
} else {
  echo 'These are different instances';
}

Comparing Properties and Methods

When we need to compare objects based on their properties and methods, we may use the built-in PHP functions get_object_vars and get_class_methods.


$varsObject1 = get_object_vars($object1);
$varsObject2 = get_object_vars($object2);

if($varsObject1 === $varsObject2) {
  echo 'Objects have the same public properties and values';
} else {
  echo 'Differences in properties or values were found';
}

$methodsObject1 = get_class_methods($object1);
$methodsObject2 = get_class_methods($object2);

if($methodsObject1 === $methodsObject2) {
  echo 'Objects have the same methods';
} else {
  echo 'Differences in methods were found';
}

Deep Comparison with Custom Function

For a detailed comparison, you can write a custom comparison function that recursively compares each property of the objects.


function deep_compare($obj1, $obj2) {
  if(get_class($obj1) !== get_class($obj2)) {
    return false;
  } foreach (get_object_vars($obj1) as $prop => $val) {
    if(is_object($val)) {
      if(!deep_compare($val, $obj2->$prop)) {
        return false;
      }
    } elseif ($obj1->$prop !== $obj2->$prop) {
      return false;
    }
  }
  return true;
}

This deeper comparison comes in handy when you have complex objects with nested objects as properties.

Using Serialization

Another way to compare objects is by serializing them with the serialize() function. This will translate the object’s structure into a string, which you can compare easily.


if(serialize($object1) === serialize($object2)) {
  echo 'Serialized objects are identical';
} else {
  echo 'Serialized objects are different';
}

Conclusion

In conclusion, PHP provides multiple ways to compare objects, each serving different use-cases, from a simple reference check to deep property comparison. Whether it’s a straightforward comparison or a detailed limitation, you now have the tools and knowledge to make informed decisions in object comparison within PHP applications.