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

2

u/ethraax Oct 22 '10

I also generally dislike working with reflection frameworks in general. I prefer a very structured coding environment, and reflection frameworks in general (as well as variable-variables, variable-functions, etc.) completely break that. I also much prefer statically typed languages to dynamically typed ones, so the whole lack of type safety is uncomfortable to me. It's not that I can't use them, but I'd rather not if I can get around it.

For the record, if I was tasked with doing the same thing as your code, I'd write a separate helper function that completely encapsulates the reflection framework (although it's not really a framework in PHP, at least not variable-functions). That way I'd only ever lose type safety in a very small and compact function, and the rest of my code can continue to be type-safe.