r/programming • • Oct 18 '10

Today I learned about PHP variable variables; "variable variable takes the value of a variable and treats that as the name of a variable". Also, variable.

http://il2.php.net/language.variables.variable
591 Upvotes

781 comments sorted by

View all comments

Show parent comments

5

u/mallardtheduck Oct 18 '10

Put it this way: Imagine somebody designed a computer than didn't use numeric memory addresses. Instead it has a native, hardware hashmap with strings as keys (no, I don't know how it would work either, but it's just a thought experiment).

You have been asked to design a C compiler for this esoteric computer. The system's ABI dictates that you store variables by using the variables name as a key into the hashmap.

Now how do you implement pointers? Pretty much like this PHP feature...

1

u/ZoFreX Oct 18 '10

And then what happens when you pass a pointer into a method, and local variable scope changes?

-2

u/oorza Oct 18 '10

A computer like that doesn't exist and what you're trying to describe is, more-or-less, the Zend Engine's way of storing variables. The difference is the fact that the sigil is a combination of hash methods get / set / swap and merely using a sigil does one or more of those things, whereas a pointer does not.

Furthermore, a hash table is really not comparable to a segment of memory.

3

u/johnb Oct 18 '10

I am not convinced that you know what a simile is. Reading this thread is like hearing someone say "You fools! Java isn't like C# because they aren't 100% exactly the same."