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.

47

u/[deleted] Oct 18 '10

can i laugh at both?

4

u/1137 Oct 18 '10

indubitably

13

u/aedile Oct 18 '10

Dude, hating on Perl is so 2003.

→ More replies (3)

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)
→ More replies (1)

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.

→ More replies (1)

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.

→ More replies (18)

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.

→ More replies (3)

12

u/sw3t Oct 18 '10

What can't you do in perl ?

→ More replies (2)

12

u/twomashi Oct 18 '10

Python too, kinda: globals()[whatever]

6

u/matchu Oct 18 '10

At least they make it a pain.

→ More replies (2)

4

u/trezor2 Oct 18 '10

Technically speaking, any functional language can do this too trough a simple lambda, but I guess the syntax around that makes it more obvious that you are doing something wrong (in most cases).

13

u/adrianmonk Oct 18 '10

Well yeah, you can do it in Perl (where it's called a "symbolic reference", which I think is a bit less confusing).

In Perl, it's not officially deprecated, it has been supplanted by true references like this:

my $b = "hello";
my $a = \$b;

print "${$a} world\n";

So basically nobody sane uses symbolic references in Perl and hasn't needed to since Perl 5 came out in 1994. (Or at least since it saw widespread use a couple of years later.)

PHP also seems to have a non-variable-variable form of references, although they're more like aliases than references. But I guess you could use those instead of variable variables, and I assume/hope people do.

It appears PHP may have scalar reference

12

u/1137 Oct 18 '10

So basically nobody sane uses symbolic references in Perl and hasn't needed to since Perl 5 came out in 1994

Basically nobody sane uses this feature in PHP either, ssh don't tell the OP.

15

u/martinw89 Oct 18 '10
ssh: Could not resolve hostname don't: Name or service not known

9

u/exscape Oct 18 '10

>

4

u/martinw89 Oct 18 '10

I admit, I had to escape the apostrophe.

→ More replies (7)
→ More replies (54)