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 Show
<?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
<?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:
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.
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.
|