PHP Variable Storage & Object Comparison - Zend Value (zval) - Full PHP 8 Tutorial
Summary
TLDRThis video script explores object comparison in PHP, explaining the use of double and triple equal signs for equality checks. It clarifies that the double equals checks for property value equality within the same class, while triple equals verifies if two variables reference the same object instance. The script also delves into PHP's memory management, explaining how variables and objects are stored, and the implications of variable assignment on object references. It concludes with a discussion on recursive comparisons and the potential for circular references to cause errors, providing a comprehensive guide for PHP developers.
Takeaways
- 🔍 PHP uses the double equal sign (==) for a loose comparison of object properties, which can result in unexpected outcomes due to type juggling.
- 🔑 The triple equal sign (===) in PHP is used for a strict identity check, ensuring both the object and its properties are identical.
- 📚 PHP's comparison operators check if objects are instances of the same class and have the same property values for the double equal sign, but only if they are the exact same instance for the triple equal sign.
- 🔄 When assigning one object to another, PHP copies the reference, not the object itself, which means changes to one object's properties will reflect in the other.
- 💡 PHP stores variable names in a symbols table, separate from their values, which are stored in a zval container.
- 📦 For objects, the zval container holds an object identifier pointing to the actual object stored elsewhere in memory.
- 🔄 Assigning an object to another variable creates a new variable pointing to the same zval container, which in turn points to the same object in memory.
- 🔄 Changing a property of an object affects all variables pointing to that object, demonstrating PHP's pass-by-value behavior for objects.
- 🔄 Extending a class does not affect object identity; an object of a subclass is not considered equal to an object of the parent class for identity checks.
- 🔍 Recursive property comparison in PHP can lead to issues like fatal errors when dealing with circular references.
- 📝 PHP allows the use of greater or less than signs to compare objects, which stops at the first property that fails the check.
Q & A
How does PHP determine if two objects are equal using the double equal sign (==)?
-In PHP, the double equal sign (==) checks for equality by comparing if the two objects are instances of the same class and have the same property values. It performs a loose comparison, which can result in unexpected outcomes when types do not match strictly.
What does the triple equal sign (===) operator do when comparing two objects in PHP?
-The triple equal sign (===) is the identity operator in PHP. It checks if two objects are not only equal in value but also refer to the exact same instance in memory.
Why would a comparison using the double equal sign (==) fail even if two objects have the same property values?
-A comparison using the double equal sign (==) can fail if the objects are not of the same class instance, even if their property values are identical.
What is the difference between an object comparison using the double equal sign and the triple equal sign in PHP?
-The double equal sign checks for value equality and type compatibility, while the triple equal sign checks for both value equality and identity, ensuring the two variables point to the exact same object in memory.
How does PHP handle type casting during a loose comparison with the double equal sign?
-PHP performs type juggling during a loose comparison, attempting to match different data types. For example, it might consider the integer 1 and the boolean true to be equal.
What happens when you assign one object to another in PHP, like assigning invoice1 to invoice3?
-When you assign one object to another in PHP, you are not duplicating the object; instead, you are creating a new reference that points to the same object in memory. Changes to the object through either reference will be reflected in all references.
Can you explain the concept of 'copy on write' in the context of PHP objects?
-Copy on write is a technique where a copy of an object is only created when it is modified. In PHP, when you assign an object to another variable, you are not creating a copy of the object; you are copying the reference to the same object. Modifications will affect all references to that object.
What is a 'zval' in PHP, and how does it relate to object storage?
-A 'zval' is a data structure in PHP used to store information about a variable, including its type and value. For objects, the zval contains an object identifier that points to the actual object stored in a separate data structure.
Why might recursive comparison of objects in PHP cause fatal errors?
-Recursive comparison can cause fatal errors if there is a circular reference between objects, leading to an infinite loop during the comparison process.
How can you compare objects using greater or less than signs in PHP?
-In PHP, you can use greater or less than signs to compare objects by their property values. The comparison stops and returns false at the first property that does not match.
What is the role of the 'symbols table' in PHP variable storage?
-The 'symbols table' in PHP is a data structure that stores variable names separately from their values. Each entry in the symbols table points to a zval container, which holds the actual value of the variable, including objects.
Outlines
🔍 PHP Object Comparison Basics
This paragraph introduces the concept of object comparison in PHP. It explains the use of the double equal sign (==) for a simple comparison, which checks if two objects are instances of the same class and have identical property values. The triple equal sign (===) is used for identity comparison, which only returns true if the two objects are the exact same instance. The script demonstrates this with two 'invoice' objects, showing that different property values result in both comparison operators returning false. It also touches on the concept of loose comparison and type juggling in PHP, which can lead to unexpected results when comparing different data types.
📚 Deep Dive into PHP Object Identity and References
This section delves deeper into how PHP handles object identity and references. It clarifies that assigning one object to another (e.g., invoice3 = invoice1) does not create a new object but rather a new reference to the same object in memory. This is why both the double and triple equal signs return true when comparing the two variables. The paragraph also discusses the concept of 'zval', a data structure in PHP that stores information about the variable type and value, and how objects are stored within this structure. It further explains the implications of this behavior, such as changing a property of one reference affecting all references to the same object.
🔄 Understanding Class Hierarchies and Object Comparison
This paragraph explores the impact of class hierarchies on object comparison in PHP. It demonstrates that an object of a subclass (e.g., 'custom invoice' extending 'invoice') is not considered equal to an object of the parent class using either comparison operator, even if they have the same property values. The summary also addresses the recursive nature of object comparison when properties are objects themselves, and how circular references can lead to fatal errors due to infinite recursion during comparison.
Mindmap
Keywords
💡Equality Comparison
💡Identity Operator
💡Loose Comparison
💡Type Hinting
💡Object Properties
💡Object Instances
💡Memory Management
💡Zval Container
💡Circular References
💡Custom Classes
Highlights
Exploring object comparisons in PHP using the double equal sign (==) for simple comparison and the triple equal sign (===) for identity.
Double equal sign checks if two objects are instances of the same class with the same property values.
Triple equal sign only returns true if two objects point to the same instance.
Demonstration of how two different objects with different property values result in false comparisons.
Loose comparison with the double equal sign can lead to unexpected results due to type casting.
Changing property values to match results in a true comparison with the double equal sign, despite loose typing.
Assigning one object to another creates a new pointer to the same object, affecting both variables when the object's properties are changed.
Introduction to PHP's memory management and how variables and objects are stored in the symbols table and zval containers.
Explanation of how PHP handles object assignment by copying the zval container, leading to shared object references.
Demonstration of the effects of changing a property in one object that is reflected in another due to shared references.
Comparison of objects from different classes, even if one extends the other, results in false with both comparison operators.
Recursive comparison of nested object properties in PHP can lead to circular references and potential fatal errors.
The impact of circular references in object comparisons and how it can cause PHP to encounter fatal errors.
Mention of the possibility to use greater or less than signs to compare objects based on property values.
Announcement of an upcoming in-depth lesson on PHP's memory management, garbage collection, and how variables and objects are stored.
Invitation for viewers to provide feedback on the teaching style and the inclusion of advanced topics in future lessons.
Transcripts
[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
Weitere verwandte Videos ansehen
Life Cycle & Reference Counting | Godot GDScript Tutorial | Ep 1.2
Arrays & Dynamic Memory Address | Godot GDScript Tutorial | Ep 10.1
Class Objects & Constructors | Godot GDScript Tutorial | Ep 16
Python OOP Tutorial 1: Classes and Instances
Object Oriented Programming Features Part 4 | C ++ Tutorial | Mr. Kishore
How Passing Arguments Works_ Value vs Reference | JavaScript 🔥 | Lecture 120
5.0 / 5 (0 votes)