A possible compromise...
Add a deref interpolation SC:
deref(string, [flags=0])
string should be a literal (in single quotes) to avoid interpolation of the function's input (see examples).
Flags:
0 = Interpolate derefs and then other variables.
1 = Interpolate derefs only.
Code: Select all
"Examples"
$g = 'Hello';
$v = '$n';
*$v = 'world';
echo deref('$g *$v!'); // 'Hello world!'
echo deref('$g *v!', 1); // '$g world!'
// Examples of why you'll want to avoid the initial interpolation.
echo deref("$g *$v!", 1); // 'Hello *$n!'
// Became deref('Hello *$n', 1);
// Flag is 1, so '$n' is untouched & there is no '*$n'.
echo deref("$g *$v!"); // 'Hello *world!'
// Became deref('Hello *$n');
// Flag is 0, there is no '*$n' so '$n' is resolved.
Since that's a new SC it shouldn't break anything - it's not as legible but it's much safer.
For HEREDOC support you introduce a new syntax '<<<*FOO' and '<<<*#FOO'. Unfortunately, this will break backwards compatibility, but I see no other way. Hopefully this won't actually affect anyone since it seems most people stick with alpha characters when naming their HEREDOCs. (I have zero matches for '<<<*'.)
Fair tradeoffs?
----
If you wanted to go even further - I know we've seen some requests for fine tuning which variables are resolved so this deref SC could really be an interpolate SC with flag:
-1 - All (default)
0 - None
1 - XY variables ('<...>')
2 - Environment variables ('%...%')
4 - Local variables
8 - Permanent variables
16 - Global variables
32 - Derefed variables.