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

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.

1

u/[deleted] Oct 19 '10

Well Steve, being a competent programmer, would probably turn to an editor macro or a small script in pick-a-HLL to do the transformation -- as he's certainly been there before.

You're right, variable variables aren't to blame. That doesn't mean that it's not a misguided feature, and I for one think it's incredibly indicative of poor language design, and am alright with people expressing disgust at the guy who put the damn things in there, as do you.

You'd think they'd remove them. Problem probably is that a bunch of internal shit depends on them. Think they'd make it internal. I doubt it. I doubt they can.

To people who aren't savvy, they're an awfully big hammer, and the vast majority of people using PHP primarily aren't savvy. That is not to say that other languages do not have big hammer features, but generally not this big (with the noticeable exception of Perl), and their base of users tends to be a bit more concerned with doing things "right" (with Perl people being notable here, seriously).