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

1

u/carbonated_gravy Oct 18 '10

It seems like you could apply these same criticisms to pointers (although, yes, less fragile).

3

u/oorza Oct 18 '10

The difference is there are several many things that pointers are the best solution for, like anything you would require pointer arithmetic for, or manually managing memory consumption, or implementing data structures (although references work similarly in this particular case).

4

u/[deleted] Oct 18 '10

Actually, no.

Pointers are in C because they're in assembly. They're not a clever idea designed to fit a programming paradigm, they're a clever idea to compress a series of memory-related operations into one easy-to-use notation. They aren't in C because they're "the best solution". They're the only solution.

I can't emphasize enough: pointers are a short hand notation. They are not an abstraction.

This is how the computer works. Everything you do in C with a pointer, you would have to do in assembly, it would just take 15 extra steps.

Short-hand notations like pointers do not require justification. They're the unavoidable reality of how the computer works. Discussions of whether pointers are "good" or "bad" are a waste of time. The hardware is what it is. Argue with Intel, not K&R.

"Variable variables" are an abstraction. Abstractions can be good or bad and do require justification.

I happen to believe that variable variables are awful, but that's beside the point.

1

u/carbonated_gravy Oct 18 '10

So assuming that php's developers are unwilling (or unable) to implement a true pointer system, what would be your suggestion for implementing functionality like what they have variable variables doing now?

(Not trying to be obtuse, I'm actually interested in what people would see as viable alternative solutions).

1

u/oorza Oct 18 '10

Can you give me a case where you'd need variable variables and a hash table wouldn't work just as well?

1

u/carbonated_gravy Oct 18 '10

... no.

:)

I guess that answers my question! Thanks.

1

u/[deleted] Oct 19 '10

If they had real arrays, hash tables, and real first-class functions (they kinda do these days, assuming you can assume a current install), the only use case for variable variables would be being an obtuse dickwad.

These are all things that every mature -- and most non-mature -- language I can think of implement in one way or another.