I'm confused about the concept of variable in PHP.
As far as I know, a variable in PHP has a name ($p
) and a value ('Carlo'
).
$p
has an associated entry in symbol table, such an entry actually points to the memory area where the variable's value is stored (i.e. the string 'Carlo'
).
Consider the following:
$n =& $p
$n
basically is an alias of $p
therefore it points to the memory area where $p
points to.
From an "under the hood" viewpoint, does the PHP interpreter create a new entry in symbol table for $n
or since it is an alias doesn't have its own symbol table's entry ?
Btw, suppose to define class Alpha
with its properties and methods. Then does $c = new Alpha
actually return a "reference" into the $c
variable, in other words is $c
's value a reference in memory to an instance of class Alpha
?
AFAIK PHP variables' values are in so called ZVALs so perhaps a Zend Value sort of thing.
It keeps a reference counter and this is also in use for the garbage collection.
Therefore the entry in the variable table I'd guess will point to a ZVAL structure and not the memory of the value in my non-internal view.
So $n =& $p
is more an alias than a reference, it does not point to memory (as a pointer in C would do AFAIK). So it points to the ZVAL and PHP manages the reference counting there for the garbage collection.
Now for the objects (class instances), IIRC, the ZVAL has an ID to the actual object. This makes it lightweight to pass around in function and method calls. And it allows to modify objects at a distance, similar to a global variable but without the effect on the global variable table (not inherently always, if you put objects into the global table, this naturally remains).