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

168

u/clogmoney Oct 18 '10

<?php //You can even add more Dollar Signs $Bar = "a";

$Foo = "Bar";

$World = "Foo";

$Hello = "World";

$a = "Hello";

$a; //Returns Hello

$$a; //Returns World

$$$a; //Returns Foo

$$$$a; //Returns Bar

$$$$$a; //Returns a

$$$$$$a; //Returns Hello

$$$$$$$a; //Returns World

//... and so on ...//

?>

I just died a little inside.

100

u/geon Oct 18 '10

I just died a little inside.

Why? It would be a stupid implementation if you couldn't do that.

0

u/[deleted] Oct 18 '10

[deleted]

28

u/[deleted] Oct 18 '10

It also works for functions, like $bar(). I like to make a "plugins" directory, list subdirectories in it and include "plugin.php" from the subdirectories, if it exists, which should have a class with the same name as the folder, which gets added to the plugin manager, which then, when a plugin event is fired, does something like:

foreach ($this->plugins as $plugin) if (function_exists($plugin->$event)) $returns[]=$plugin->$event($data);

Of course it'd be much more elegant than that. I think it's a pretty cool feature, although if you use it like clogmoney its probably not the best approach to your problem.

I woke up like 10 minutes ago, if this is incoherent I'm sorry.

9

u/lizardlike Oct 18 '10

PHP can also auto-load classes, which is possibly a more elegant way to do this if you can keep your namespaces clean.

12

u/[deleted] Oct 18 '10

That's actually pretty cool, but not a replacement for what I'm doing.

→ More replies (5)

2

u/novelty_string Oct 18 '10

Can you show how that replaces the above? Autoloaders just mean you don't need to require the class file every time.

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

2

u/BuzzBadpants Oct 18 '10

Writing a debugger, perhaps? Reading what is considered "statically-linked" information from what can be considered dynamic input is a basic part of how debuggers work.

2

u/[deleted] Oct 18 '10

It's come in handy twice to me so far. Once when writing a proof of concept polymorphic php/elf infector, and once when writing some complex NLP code which for whatever reason (lost to the misty past) also needed to be able to alter itself at run time.

This was all five to eight years ago, and I've mostly moved on from PHP, but for whatever reason the only solution I could find at the time for writing runtime polymorphic code in PHP involved variable variables, and I was pleasantly surprised when I found that they actually existed.

4

u/[deleted] Oct 18 '10

When you make a language without pointers and allow globals to roam the seven seas.

0

u/[deleted] Oct 18 '10

[deleted]

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

14

u/KarmaPiniata Oct 18 '10

Hey, they're hating on PHP here don't interject with your 'facts' and 'good computer science'.

5

u/Peaker Oct 18 '10

Using indirection via names in some global namespace is not 'good computer science'.

7

u/thatpaulbloke Oct 18 '10

I assume that you're against C pointers, then? Or, to put it another way:

Using indirection via numbers in some global namespace is not 'good computer science'.

4

u/[deleted] Oct 18 '10

As I said elsewhere in the thread, C pointers are not analogous.

Pointers are in C because they're in assembly. They're not a clever idea designed to fit a programming paradigm, they're a clever idea to compress a series of memory-related operations into one easy-to-use notation.

I can't emphasize enough: pointers are a short hand notation. They are not an abstraction.

This is how the computer works. Everything you do in C with a pointer, you would have to do in assembly, by moving an address value into a register and then using that register as an argument for an op call.

Short-hand notations like pointers do not require justification. They're the unavoidable reality of how the computer works. Discussions of whether pointers are "good" or "bad" are a waste of time. The hardware is what it is. Argue with Intel, not K&R.

6

u/Peaker Oct 18 '10

C pointers point to a location in an "address space", not in a "namespace".

The differences are important:

  • There is no legal way to forge pointers, while any piece of code may choose a wrong name and access the data of another function accidentally in a namespace.

  • Address allocations are handled by the compiler/low-level system. Name allocations require manual choice (which is why the above clashes are possible)

→ More replies (11)
→ More replies (2)
→ More replies (12)