How can you compare objects in php?

You can use the various comparison operators PHP makes available on objects, but their behavior is worth noting. Let’s take a closer look at the behavior of the comparison == and identical === operators in the context of objects:

  • Comparison operator ==
    This works on objects by making sure that both objects are of the same type and have the same values for their member data. If either of these is not true, the operator evaluates to FALSE.
<?php
 Class User {
  protected $name = 'Guest';
  protected $country = 'US';
  function __construct($n, $c) {
   $this->name = $n;
   $this->country = $c;
  }
  public function getName() {
   return $this->name;
  }
  public function getCountry() {
   return $this->country;
  }
 }
 
 $a = new User('Brainbell', 'US');
 $b = new User('Brainbell', 'US');
 $c = new User('Brainbell', 'UK');
 
 var_dump($a == $b); # bool(true)
 # same type and have the same values
 
 var_dump($c == $b); # bool(false)
 # same type but different values
  • Identity operator ===
    This works on objects by indicating if the two objects being compared are the same instance of the class. Otherwise, it evaluates to FALSE.
<?php
 // ...
 $a = new User('Brainbell', 'US');
 $b = new User('Brainbell', 'US');
 
 var_dump($a == $b);  # bool(true)
 # same type and have the same values
 
 var_dump($a === $b); # bool(false)
 # different instance of the class
 
 $c = $b;
 
 var_dump ($c === $b);
 # the same instance of the class

For more information, visit https://php.net/manual/language.oop5.object-comparison.php.


PHP OOP Tutorials:

  1. PHP Object Oriented Programming
  2. PHP Class Constructor and Destructor
  3. PHP OOP Encapsulation and Visibility Modifiers
  4. PHP Class Constants
  5. PHP Static Variables and Methods
  6. PHP OOP Inheritance

  1. PHP Abstract Classes and Methods
  2. PHP OOP Interfaces
  3. Abstract Classes vs. Interfaces
  4. PHP Object Cloning
  5. PHP Comparing Objects
  6. PHP OOP Polymorphism

  1. PHP OOP Composition and Aggregation
  2. PHP Traits
  3. PHP Closure Class
  4. PHP Namespaces
  5. PHP Autoloading Classes

PHP allows you to compare objects with the comparison and the identity operators. When using the comparison operator, PHP checks to see if two objects have the same attributes and values, and if they’re instances of the same class. With the identity operator, you check to see if the object variables point to the same instance of the same class.

Recap: Review the code from the previous article.

Aside from the Lamborghini class, we’re going to recreate the Ferrari class that we once had.

Both of these classes extend the Car class. The only difference is their name. Now it’s time for some testing. Brief and to the point.

We’ll create two Lamborghini objects, $lamborghini_1 and $lamborghini_2. They’ll be instantiated with the exact same constructor arguments. We’ll then create $lamborghini_3 and set it equal to $lamborghini_2. Let’s see the results that we get when comparing these 3 objects. We’ll use the comparison operator (==) and the identity operator (===) for these tests.

  • var_dump( $lamborghini_1 == $lamborghini_1 ) yields true. This is expected since we’re looking at the same object. But, let’s look at the identity operator next and see if we get the same result.
  • var_dump( $lamborghini_1 === $lamborghini_1 ) yields true. This is another expected result. These 2 objects are in fact the same object, so of course it would be true.
  • var_dump( $lamborghini_1 == $lamborghini_2 ) yields true. After all, both objects are instantiated using the same constructor arguments so reason would dictate that they are equal. But are they identical? Let’s look at that next.
  • var_dump( $lamborghini_1 === $lamborghini_2 ) yields false. These two objects are not identical since they’re not the same object. The variables are pointing to different areas within memory.
  • var_dump( $lamborghini_2 === $lamborghini_3 ) yields true. This is interesting. They have different variable names but they’re actually pointing to the same area in memory, making them identical. When you assign a variable to another object, it’s not making a copy of the object, it just assigns a reference to that object’s memory location. This means that if you modified a property within the $lamborghini_3 object, you would also be modifying $lamborghini_2 since they point to the same object in memory. Let’s see that with an example.

We’re modifying the year of the $lamborghini_3 object and we’re using var_dump to display the year of the $lamborghini_2 object. Should the result be 1999 as was instantiated or 1998 for $lamborghini_2? The answer is 1998. Remember, both $lamborghini_2 and $lamborghini_3 point to the same object in memory.

Let’s look at another example with different classes. We’ll create a Ferrari object with the same constructor arguments as our Lamborghini object and see if we can trick PHP into thinking it’s the same class.

And the result we get is…false. PHP is smart enough to know that these are two difference objects even though they both extend the Car class. The class names are different so the objects are different in PHP’s eyes.

Dino Cajic is currently the Head of IT at LSBio (LifeSpan BioSciences, Inc.), Absolute Antibody, Kerafast, Everest BioTech, Nordic MUbio and Exalpha. He also serves as the CEO at MyAutoSystem. He has over a decade of software engineering experience. He has a B.S. in Computer Science and a minor in Biology. His background consists of creating enterprise level e-commerce applications, performing research based software development, and facilitating the spread of knowledge through writing.

You can connect with him on LinkedIn, follow him on Instagram, subscribe to his blog, or subscribe to his Medium publication.

Read every story from Dino Cajic (and thousands of other writers on Medium). Your membership fee directly supports Dino Cajic and other writers you read. You’ll also get full access to every story on Medium.

How do you compare two objects?

Whereas the equals() method compares two objects. Objects are equal when they have the same state (usually comparing variables). Objects are identical when they share the class identity. For example, the expression obj1==obj2 tests the identity, not equality.

How do you compare values in objects?

isEqaul is property of lodash which is used to compare JavaScript objects. It is used to know whether two objects are same or not. For ex, there are two arrays with equal number of elements, properties and values are also same. Even the properties are not in same order it will return true.

Can you use == to compare objects?

Comparing Objects ¶ 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.

What is a function to comparing the objects?

Comparing objects is easy, use === or Object.is(). This function returns true if they have the same reference and false if they do not.