PHP Variable Storage & Object Comparison - Zend Value (zval) - Full PHP 8 Tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] how can we compare two objects for equality in php let's dive right in and explore how object comparisons work in php i have two objects here of the invoice class where the invoice class just has two properties the amount and the description then in the index.php i'm comparing invoice 1 to invoice2 using the double equal sign also known as the comparison operator and then i'm comparing invoice 1 to invoice 2 using the triple equal sign also known as the identity operator the comparison operator does the simple comparison the two objects will be equal if they are instances of the same class and have the same properties and values in the case of the identity operator though two objects will only be equal if they point or refer to the same instance of the same class so in the current example we have two objects that have different values for the properties and therefore the double equal sign check should fail and then triple equal sign should also fail because these two objects are not the same object they're two different objects so if i run the code sure enough we get false for both comparisons something to note with the double equal sign comparison is that it does the comparison of the property values using the loose comparison what i mean by that is that let's say that instead of 25 we passed in one and instead of 100 we passed in true and we don't have the strict types enabled so even though we are type hinting amount to be float because we don't have the streak types enabled php will still try to figure out the type on its own and do its own casting behind the scenes so essentially what double equal sign does here is that it does this kind of comparison one double equals true and when we compare one to true in this manner this will return true so if i run the code now we're still getting false the reason for that is because the description property here is different let's remove that and now the description also matches and if we run the code again and now it returns true when we compare invoice one to invoice two even though 1 and true are not exactly the same so let's change this back to 25 and let's also set this to 25 and now we have two different objects but they have the same property values if i run the code same as before we're going to get the true for the first comparison and we're going to get the false for the second comparison so why are we getting false for the second comparison the reason for that is because invoice 1 and invoice 2 are two different objects they're pointing to two different parts in the memory they're not pointing to the same object in the memory let's look at the next example let's say that we have invoice three that equals to invoice one now let's change this comparison to compare invoice 1 with invoice 3 instead of the invoice 2 and see what we get so now we're simply comparing invoice 1 object with invoice 3. let's clear this out run the code and we're getting true for the both cases that's because invoice 3 is actually the same object as invoice one it simply points to the same objects that invoice one points to in memory invoice one and invoice three variables here are simply pointers that point to some spot in the memory that contains that object and therefore when we assign invoice 1 to invoice 3 we're not actually duplicating or copying or cloning this object we're simply creating a copy of a pointer that will point to the same location in the memory that contains that object there are a lot more details about this and i'm going to go over some of those details in this lesson but it is a bit complicated topic itself on how variables references and objects are stored in php behind the scenes so i'm not going to go into a lot of detail in this lesson about it however i'm working on a separate video to explain this in more detail including memory management garbage collection references variables and how they're stored and so on so stay tuned for that video but let me show you and explain in simple terms what happens in php when you create variables like this and then when you assign a variable to another variable like this so when a variable is created in php php will store the actual variable name separately from its value the variable name is stored into something called the symbols table so essentially a variable is just a symbol and every variable has an entry in the symbols table each entry in the symbols table points to some sort of container or data structure that contains the value of the variable so you can think of the variable as just a pointer that points to some sort of data structure this container or the data structure where the value is stored is called a z val which is short for zen value and is a c language structure it stores information about the variable like the type and value and we're not going to go in a lot of detail on this because as i mentioned before we'll cover that in a separate video now the way objects are stored in this zval container is a bit different than the way other simple types like integers are stored values for simple types like integers are stored directly in the xero container but for objects it only stores an object identifier which again is just a pointer to another complex data structure or an object store that contains the actual object now when we assign invoice 1 to invoice 3 we are again creating a new variable invoice 3 which gets added to the symbols table and xeval container is simply copied from the invoice 1 and invoice 3 will now point to that new zevo container the thing is though both zival containers contain the same object identifier in the value so they indeed point to the same location where the actual object is stored this is why triple equal comparison is passing because it's the same object even though there are different variables as shown in this diagram they still point to the same object we can further prove this and simply var dump both invoice 1 and invoice 3 and run the code and we see that they are indeed the same object in fact if we try to change the property on invoice 3 let's say we change the amount to something like 250 this would also affect invoice one so if we clear this out and run it again we see that both invoice one and invoice three have the property set to 250. and again this is because as we established in the diagram both invoice one and invoice 3 are pointing to the same object in memory so making changes affects all variables that point to the same object this is why it is sometimes said and believed that objects by default are passed by reference which is actually not true and objects by default are passed returned and assigned by value as to why and how we'll cover that in that separate video which will be more in depth with a lot of details about memory management garbage collection how variables references and objects are stored behind the scenes and so on so don't worry about the zen value container and object location in memory or symbols table or anything like that for now we'll get to that in that separate lesson i just wanted to give you a short overview of what happens behind the scenes and kind of introduce you to the topic that we'll cover in that video alright back to code let's create another class called custom invoice and create an object of that class so instead of having new invoice for the invoice 2 here we'll have new custom invoice and let's create that class and we could simply extend the regular invoice so that way we reuse the same constructor now we know that both invoice and custom invoice have the same property values because we are creating two objects with the same values right let's see what happens when we compare invoice 1 to invoice 2 using double and triple equal signs let's get rid of this let's clear this out run the code and we see that it returns false in both cases and this makes sense right especially after what we saw in the diagram the first check fails because they're not of the same instance invoice 1 is an instance of invoice class and invoice 2 is an instance of custom invoice class even though custom invoice class extends invoice class now in the case of triple equal comparison this also fails because it's not the same object this actually points to the different object and invoice 2 points to the different object and i have a typo here this is supposed to be in voice two we can even further prove that by var dumping both invoice one and invoice two and looking at their ids we see that this is an object number three and this is an object number two note that if your class contains a property that is an object of another class then comparison is done in a recursive manner for example say that invoice class had a property called customer let's say that we were accepting an object of a customer class and we define that property let's create this class also and let's say that this simply has one property called name now in the index.php we can simply create a new object here new customer customer one and let's duplicate that for the invoice too and we don't need to use the custom invoice in this case now when we clear this out and run the code let's scroll it up we see that invoice one compared to invoice two with the double equal sign returns true again but triple equal sign returns false it returns true for the double equal sign because all the property values are same however if the property value of the nested object is different then it will fail so if we change this to customer 2 for example and we clear that out and we run the code we see that now invoice 1 compared with invoice 2 with double equal sign returns false another thing to note about this is that since it does recursive comparison if you have circular relations it can cause fatal errors say that we want to have a property within the invoice class that holds another invoice object to create some sort of linking between invoices so we would have something like public invoice linked invoice and that way we can link invoices to each other we can make this nullable and set it to null by default and now in the index.php we can do something like this invoice1 linked invoice equals to invoice2 and invoice2 linked invoice equals to invoice one so as you can see we're creating this circular relationship here now when we try to compare invoice one within ways to like this and we run the code we're going to get this fatal error also before we wrap this up i want to mention is that in addition to use the equality checks like this you could also use greater or less than signs to compare objects it will stop and return false at the first property that fails the check this is it for this video as i mentioned earlier in this video i'm working on a very interesting lesson though it's a bit advanced topic and i'm not sure if i should include it in the second section of the course or wait until the third section of the course when we move on to more advanced topics let me know in the comments what you think about it if you wish to learn more about how variables are stored in php including references and objects and if you actually like that style of teaching where in addition to learning php or also learning how certain things work behind the scenes thank you so much for watching if you enjoy my lessons please give my videos thumbs up share and subscribe if you're new to the channel and i'll see you next time
Info
Channel: Program With Gio
Views: 4,909
Rating: undefined out of 5
Keywords: advanced php course, learn php the right way, object oriented php, php, php course, php in 2021, php tutorial, php oop tutorial for beginners full, php8, object comparison in php, compare objects in php, identity operator and objects, zend value container, zval container
Id: zCGmZb3z-r8
Channel Id: undefined
Length: 11min 20sec (680 seconds)
Published: Tue May 18 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.