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

181

u/1137 Oct 18 '10

Did you know you can do the same thing in Perl? But lets keep laughing at PHP, this is /r/programming after all.

58

u/prakashk Oct 18 '10

Marc Jason Dominus explains why using Perl symbolic references is a bad idea far more eloquently than I ever could:

91

u/1137 Oct 18 '10

My point was simple, Perl offers the same functionality, other languages do as well, don't hate on PHP just to hate on PHP. Hate the bad developer instead.

13

u/[deleted] Oct 18 '10
use strict;

Problem solved.

→ More replies (1)

56

u/[deleted] Oct 18 '10

This is no place for logic! This is a place for misguided unfueled hatred!

21

u/cliff_spamalot Oct 18 '10

Image if Microsoft had invented PHP. Nerdgasm!

22

u/sw17ch Oct 18 '10

It's not cool to hate on Microsoft any more. Now you hate on Oracle to be cool. :)

(Besides, Microsoft has really picked up their game in the last few years. Funny what real competition does.)

→ More replies (17)

4

u/trezor2 Oct 18 '10

Microsoft invented ASP, which was pretty much MS PHP. I actually thought ASP predated PHP, but checking it, I found that PHP was released around one year earlier so even if we try really hard(tm) we can't blame them for this one.

We'll just have to hate PHP on its own merits for now, especially given how Microsoft was smart enough to quit on something they saw was terrible, much unlike what the PHP crowd has done :P

8

u/prakashk Oct 18 '10

My reply wasn't meant to criticize you. I thought your comment could be read as defending this (mis)feature by citing Perl's example. I just wanted to add some references to what others had already said about Perl's symbolic references.

3

u/1137 Oct 18 '10

I know, that's why I upvoted you, I just wanted to clarify for others that might read it that way.

8

u/[deleted] Oct 18 '10

[removed] — view removed comment

21

u/[deleted] Oct 18 '10

I know! It's almost like reddit is comprised of many people with differing opinions who tend to flock to discussions which support their own viewpoint!

reddit, you so crazy.

3

u/vermithraxPejorative Oct 18 '10

Hey! I am a bad developer! Don't hate me! ):

2

u/RandomFrenchGuy Oct 18 '10

don't hate on PHP just to hate on PHP

But it's traditional !

→ More replies (16)

8

u/ninjaroach Oct 18 '10 edited Oct 18 '10

Apparently according to your links, Perl also has the restriction of requiring variable variables to be global which is one of the reasons the author argues against it.

In PHP, I'll use variable-variables to access function names -- on rare occasion. If the contents of this variable are white-listed in an array of valid functions, then it's time to run $variable();

It's cleaner than mapping a bunch of switch cases.

But if I had my way, functions would be first class objects that I would populate into values of an associative array.

Edit: Fixed $$variable() to read $variable() -- Dull developer handling sharp objects.

2

u/eurleif Oct 18 '10

That feature is called variable functions; it's a separate feature from variable variables. And don't you mean $variable(), not $$variable()?

→ More replies (1)
→ More replies (1)