PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
here is the output of the code above, what i don't understand is why the contents of $top [ 'A' ] [ 'parent' ] and $top [ 'B' ] [ 'parent' ] are not the same :
PHP uses a symbolic reference table to keep track of variables. If you're really interested in this, you should read up on all what ZEND has to say about variables.
As I mentioned before, PHP keeps track of variables with a symbolic reference table. If you create a variable, the actual content of that variable is stored somewhere in memory. And that content is then being referenced by a symbol. (ie. $var is a symbol, and "this" is the contents) Now the magic happens when you make a reference to another symbol. (the PHP manual calls this "Making a reference to a variable") You can do this using "$var2 = &$var". Now the contents of $var is not duplicated, yet another reference is added to the contents. The piece of memory containing "this" now has two references. And in the symbolic reference table there are two entry ($var, and $var2) which both point to the same content.
I hope this explains the use of references a bit. If you have any other question, feel free.
i've actually read a fair deal on php references and i've gotten to understand most of the little peculiarities, however the code above still escapes my understanding. as you'll see, even using the symbolic reference table, there is no apparent reason why the same reference applied twice in a row will lead to different variables. what the manual has to say about this in chapter 20 is "Complex arrays are sometimes rather copied than referenced", but it doesn't give the exact nature of the complexity nor when the "sometimes" actually occurs.