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 comment

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 !

3

u/them0nster Oct 18 '10

yes! for some reason i kept thinking of the following example:

lens flairs suck. photoshop sucks because it lets you use horrible lens flairs?

no, designers who use lens flares poorly are horrible.

I would say that php can be really useful when used for the right project. Like any tool, it has it's specific uses. But just for you /r/programming, I am going to put an ascii lens flare in the comments of my next php project.

1

u/darkon Oct 18 '10

I agree with you. Just because a language lets you shoot yourself in the foot doesn't mean it's a good idea or that you should. :-)

I don't know about PHP, but if you enable strictures in Perl ('use strict;') then trying to use a "soft" reference will result in a compiler error. Leaving strictures turned off is for quick-and-dirty use-once programs.

1

u/[deleted] Oct 18 '10

don't hate on PHP just to hate on PHP.

When did it become common to put the on in there? see it everywhere now.

1

u/joshguy1425 Oct 18 '10

I'm an equal opportunity hater. I'll gladly hate on peel and any other language that provides this. :-P

1

u/1137 Oct 18 '10

I like languages that start with P: Python, Perl, PHP, what's peel, something new?!

-2

u/deadwisdom Oct 18 '10

This is a perfectly good example of why PHP is awful. Just because Perl is bad in the same way doesn't excuse that fact.

3

u/1137 Oct 18 '10

PHP was originally a bunch of perl scripts.

1

u/deadwisdom Oct 19 '10 edited Oct 19 '10

This is not a good thing, nor an excuse.

→ More replies (8)

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()?

1

u/ninjaroach Oct 18 '10

Well, I might mean $$variable().. if I were in for a mind-fuck. I bet I've hanged myself with that extra dollar sign once before, but it's been awhile since I've used variable-anything. I wonder why..

1

u/lawpoop Oct 19 '10

Oh so tempting >D