3

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 $nor 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 ?

7
  • 1
    Does it answer your question? Commented Mar 25 at 9:54
  • 1
  • when a class is instantiated ($p = new Alpha) therefore the variable $p's value is not the instantiated object itself but it is actually the "object identifier", right ?
    – CarloC
    Commented Mar 25 at 10:28
  • So does the PHP manual say Commented Mar 25 at 10:43
  • Therefore there is a sort of indirection: the symbol table's entry for $p points to the memory location (i.e. $p's value) that actually stores a reference to the instantiated object in memory.
    – CarloC
    Commented Mar 25 at 11:01

1 Answer 1

2

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).

2
  • Ah ok, you mean after $n =& $p, $n doesn't point to the memory location that stores the actual value, it points indeed to the in memory ZVAL structure (which in turns stores the actual value). In case of class's instances (i.e. objects) the ZVAL structure stores an ID pointing to the actual object/instance in memory. Right ?
    – CarloC
    Commented Mar 25 at 15:19
  • 1
    @CarloC: Yes, this is the mental model that worked well for me. I've stumbled a bit in my archives and dug up this piece from 2010: References to the Max (hakre on wordpress). May have some link rot but archive.org may help, have not checked it in and out, but if you're in for a read, it may give you some more infos and resources. I think the phpinternals book referenced in comments above is a good ref.
    – hakre
    Commented Mar 25 at 17:32

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.