Uniform Variable Syntax was voted in (almost unanimously) for PHP6 PHP7 and introduces a rare back compatibility break, changing the semantics of expressions like these:
// old meaning // new meaning 1. $$foo['bar']['baz'] ${$foo['bar']['baz']} ($$foo)['bar']['baz'] 2. $foo->$bar['baz'] $foo->{$bar['baz']} ($foo->$bar)['baz'] 3. $foo->$bar['baz']() $foo->{$bar['baz']}() ($foo->$bar)['baz']() 4. Foo::$bar['baz']() Foo::{$bar['baz']}() (Foo::$bar)['baz']()
IMO the “variable variable” syntax $$name
is a readability disaster that we should get rid of. ${$name}
is much clearer about what this is (dynamic name lookup) and what it’s doing: $
is the symbol table, and {$name}
tells us that we’re finding an entry in it under a key with the value of $name
. PHP should deprecate $$name
syntax and emit a notice in the next minor version.
It should also deprecate the syntax ->$name
and ::$$name
. Both are bad news.
Doing so would completely eliminate the first 3 ambiguous expressions above, and the new warning emitted would call out code that would otherwise silently change meaning in PHP6 (one big negative about this RFC).
As for the fourth, Consider these expressions:
A. $foo->prop B. $foo->prop() C. $foo->prop['key']() D. Foo::$prop E. Foo::$prop() F. Foo::$prop['key']()
To the chagrin of JavaScript devs, PHP will not let you reference a dynamic property in an execution context, so expression B will try to call a method “prop”, (and fatal if it can’t). For consistency, PHP should fatal on expression E. Currently PHP just does something completely unexpected and insane, looking up a local variable $prop
.
Expression C does what you’d expect, accessing the property named “prop”, so expression F should do the same, which is why I think the RFC is a clear step forward at least.