5
When I have for example:
$abc = new ZZZ();
$mno = $abc;
The object $mno
is the same as $abc
? That is, do they have "memory addresses" alike? How do I print this "memory address"?
5
When I have for example:
$abc = new ZZZ();
$mno = $abc;
The object $mno
is the same as $abc
? That is, do they have "memory addresses" alike? How do I print this "memory address"?
5
First let’s understand the correct definitions. Nor $abc
, nor $mno
are objects, are variables.
Variables can store objects, or references to objects. If they store objects obviously objects are different because they are two different variables , and if read and definition of what is a variable, know that they are two completely different memory addresses.
If in the variable you have stored a reference, and you used a new
certainly has a reference, so it is possible that this reference is stored in two different variables, and if it is the same reference it points to the same object.
In case when did $mno = $abc
you took the reference that was stored in $abc
and assigned to $mno
, so the two variables have the same value at this point, and we also conclude that it is the same reference pointing to the same object. We can even conclude to be the same object for only had an object creation instruction.
There is nothing in the standard PHP API that reliably tells you what the address of an object is.
Strangely (or not, after all PHP is full of these inconsistent things) if you adopt a reference to something that is already a reference, the language disregards this reference and has no double indirect. Below there are other oddities. So only use the reference if there is no other way, and always.
Can help:
Browser other questions tagged php memory objects
You are not signed in. Login or sign up in order to post.
Thanks for the answer, it was clearer. But what if we change the second line for $mno = &$abc? What would be the differences? I was doing some tests with this new line and even so, seem to reference to the same object. So I didn’t understand the difference
– zepolik
For this case changes nothing, you already have a reference.
– Maniero
Hmm interesting. Learning a little more about PHP, thanks for the help
– zepolik