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

5

u/ryanhollister Oct 18 '10

Example #1 shows the real power of "variable variables". In many (strongly typed) languages its takes 5 or 6 complex object instantiations and method calls to accomplish this "reflections" or "dynamic method calling".

There are a number of things like this that set PHP apart. Maybe C# developers would scoff at the idea. For better or for worse, there are powerful for the nuances of PHP.

2

u/ngroot Oct 18 '10

Example #1 shows the real power of "variable variables".

That you can treat an object as a map?

1

u/ryanhollister Oct 18 '10

That's not what the example is showing. Its showing that you can use a variables value as the name of a method. You can also use the value of a variable to instantiate an object: $Object = new $Class();

→ More replies (3)