Join Kevin Skoglund for an in-depth discussion in this video Compare objects, part of PHP: Object-Oriented Programming.
- [Instructor] In this movie, we'll learn how PHP compares objects, there are two ways that we can perform comparisons. The first is using the simple comparison operator, equals equals. That's what you're probably used to most of the time. There's also a stricter version of that, which is the identity operator, and that's a triple equal sign. It goes just a little bit further, and make sure that things don't just appear to be the same, but they are actually the same. Let me show you how that plays out when we're working with objects. So with objects, the comparison operator will compare the object's properties.
So if the class is the same and the property values are the same, then they will be considered equal. So if we have variables that reference the exact same instance, or if they reference two different instances, but which both have matching properties, so if they if they seem to be identical, then it'll return true. If the objects are of a different class or they have different properties, then it's going to return false because they're not the same. It just basically does a simple class and property comparison. The identity operator, that triple equals, is going to check more than properties.
It returns true only if they both refer to the exact same object. If they're two separate instances that just happen to have the same property values, it's not going to be considered equal. What we learned about assignment reference in the last movie, it makes sense that both variables must have the same object identifier and point to the same piece of memory in PHP. Let's examine this further by looking at a code example. Inside our sandbox, I've added a new file called compare_objects.php.
And I put very simple class in here called box, and it has one property, which is name, and it's going to default to box. So I just want to have something super simple, so I could create it and reference it and clone it, all different things. First, I'm going to great a new instance of the box. I'm going to assign that to the variable box. Now, I'm going to just make a reference to that box, right? This is assignment by reference, so now box_reference is going to point to the same thing as box. We saw that in the last movie. They point to the same thing in memory because they have the same object identifier.
Next, I'm going to try cloning the box. So I have something called box_clone here. This is going to be different. It's a different piece of memory, has a different identifier for that object, and then I'm going to try another clone where I modify one of the properties. So now the properties are different as well. And last of all, let's just make a new instance of the box to compare against. So let's run a series of checks against these. The first one is going to be the simple comparison operator, the double equals, and so we're going to compare box against all four of these, box_reference, box_clone, box_modified, and another_box.
So each one of those we're going to compare against and will return true, and will echo back a T if this returns true, or an F if it returns false. So we'll be able to know whether or not it considers them to be equal. And then we'll do the same thing down here using the triple equals. That's a stricter check. It's going to check if they reference the same object. Alright, let's try it out. Let's save this file, let's come back over here, and let's do compare objects. And sure enough, when we use the double equals, three out of the four are considered to be equal.
The one that's not is the one where we modified one of the properties, because double equals only cares if they're of the same class and it has the same values for its properties. In the case of the triple equals though, notice that three out of the four came back false. The only one that is considered true is reference, the one where we actually just did an equal sign that points to the same thing, the same piece of memory. Box_reference and box both hold the same object identifier and point to the same piece of memory.
So, the triple equals says, "Yes, these are the same. "They are exactly the same." If we make a change to one, we're making a change to the other. In the case of every other one, even though they look the same, even though we duplicated them and they have the same default values, for all the other ones, including the one where we cloned it, they're not going to be considered equal.
- Defining classes
- Calling methods
- Class inheritance
- Extending and overriding classes
- Accessing and controlling access to properties and methods
- Static properties and methods
- Magic methods: constructor, destructor, and clone
- Creating a PHP OOP project