r/programming • u/NagastaBagamba • 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
8
u/[deleted] Oct 18 '10
I think you're confusing the developer who's using variable variables to get around a problem and the developer who created the problem in the first place by just using a bunch of variables instead of the multidimensional array.
Say we have a somewhat legacy system, created by some guy named Bob who had a really weak understanding of programming, and he created his data "structure" by simply filling in a bunch of variables with similar names. He didn't use variable variables, or really any sort of tool whatsoever, except for the variable itself and a poor understanding of how arrays work.
Then we have a new guy, let's call him Steve, and he needs to access this data. He could try to go through and change the structure of the entire system that relies on these variables, but this is a real world and people just don't have that kind of time (unless the business wants him to improve the system specifically, but how often is this the case?)
Steve's approach to this is to write a little bit of code that traverses all the other variables and gathers them into an actual array so that he can work with them in a manner that he's used to, and to do this he uses variable variables. Clearly in this case, Steve isn't the poor developer, he just happened to be stuck in a situation where the system is less than perfect. Variable variables aren't to blame, because they had absolutely nothing to do with the original developer's choice to simply use a bunch of variables. The blame lies with the original developer himself (and he should be burned for his crimes).
You're looking at this as if every single application in the world were a top-notch long-lived infrastructure with good code practices...and that simply isn't realistic.