rfc:deprecate_dollar_brace_string_interpolation

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
Next revisionBoth sides next revision
rfc:deprecate_dollar_brace_string_interpolation [2021/03/13 14:28] – Fix option ilutovrfc:deprecate_dollar_brace_string_interpolation [2021/03/13 18:22] – Overhaul language to make it more compelling. crell
Line 16: Line 16:
   4. Dynamic variable lookup ("${expr}")   4. Dynamic variable lookup ("${expr}")
  
-Unfortunately, all of these have their drawbacks but options 3 and 4 are by far the worst, particularly because they share the exact same syntax. This RFC proposes to deprecate options 3 and 4 in PHP 8.1 and remove them in PHP 9.0.+All of these have their pros and cons, but in practice options 3 and 4 are easily confused (both by users and the parser) due to the similar syntax, are strictly less capable than the first two options, and are rarely used in practice.  Option 3 is specifically less capable than 2, and option 4 is built on variable-variables with all the complexity and weirdness that brings with it. 
 + 
 +This RFC proposes to deprecate options 3 and 4 in PHP 8.1 and remove them in PHP 9.0.  That leaves only two string interpolation options: direct (simple variable only) and powerful (the one that does the most now).
  
 ===== Status quo ===== ===== Status quo =====
  
-==== Options 1and 3 ====+The first issue is that all four options support different subsets of syntax.  That is needlessly confusingboth for the user and for the parser.
  
-Options 1, 2 and 3 all roughty try to do they same thing: They embed a local variable in a string, sometimes allowing to call a method or access an array offset on that variableAll of these examples are equivalent.+==== Simple variable interpolation ==== 
 + 
 +Options 1, 2and 3 support embedding basic variables Option 4 has no equivalent here.
  
 <code php> <code php>
 $foo = 'foo'; $foo = 'foo';
 +
 var_dump("$foo"); var_dump("$foo");
 var_dump("{$foo}"); var_dump("{$foo}");
Line 31: Line 36:
 </code> </code>
  
-All of the three options allow accessing an array offset. Unfortunately, the syntax is not consistent.+==== Array-offset interpolation ==== 
 + 
 +Options 1, 2, and 3 allow accessing an array offset. Unfortunately, the syntax is not consistent.
  
 <code php> <code php>
 $foo = ['bar' => 'bar']; $foo = ['bar' => 'bar'];
 var_dump("$foo[bar]"); var_dump("$foo[bar]");
 +
 var_dump("{$foo['bar']}"); var_dump("{$foo['bar']}");
 var_dump("${foo['bar']}"); var_dump("${foo['bar']}");
 </code> </code>
 +
 +==== Object property interpolation ====
  
 Only syntax 1 and 2 allow accessing properties. Only syntax 1 and 2 allow accessing properties.
Line 44: Line 54:
 <code php> <code php>
 $foo = (object) ['bar' => 'bar']; $foo = (object) ['bar' => 'bar'];
 +
 var_dump("$foo->bar"); var_dump("$foo->bar");
 var_dump("{$foo->bar}"); var_dump("{$foo->bar}");
 </code> </code>
 +
 +==== Metehod call interpolation ====
  
 Only syntax 2 allows calling methods. Only syntax 2 allows calling methods.
Line 60: Line 73:
 var_dump("{$foo->bar()}"); var_dump("{$foo->bar()}");
 </code> </code>
 +
 +==== Compound interpolation ====
  
 Only syntax 2 allows chaining all of the above. Only syntax 2 allows chaining all of the above.
Line 107: Line 122:
  
 The braces switch from option 3 to 4 because braces are not allowed in option 3. This means ''foo'' is no longer interpreted as a variable but as a constant, and option 4 will then try to find a local variable by the name of that constant. This is incredibly unintuitive. The braces switch from option 3 to 4 because braces are not allowed in option 3. This means ''foo'' is no longer interpreted as a variable but as a constant, and option 4 will then try to find a local variable by the name of that constant. This is incredibly unintuitive.
 +
 +==== Comparison to other languages ====
 +
 +A number of other languages use ''%%${foo}%%'' style string interpolation, most notably bash and Javascript (in template literals).  However, its behavior is different from that in PHP.  In PHP, that syntax means variable-variables.  In Javascript, it supports arbitrary expressions (making it technically a superset of PHP's existing option 2).  In its current form, options 3 and 4 are of limited use, and confusing for users from other nearby languages as they behaves quite differently.
  
 ===== Conclusion ===== ===== Conclusion =====
  
-Options 1, 2 and 3 all try to achieve the same thing. Option 1 is less powerful than option but it is shorter so both have some justification for existing. Option 3 is less powerful than both while not being shorter than any of them. Option 4 is not commonly useful. Option 3 and 4 clash syntax-wise.+Option offers a simple "base case" for the most common situation and is widely used. 
 + 
 +Option 2 offers the most robust syntax currently supported, and is widely used. 
 + 
 +Option 3 offers a subset of the functionality of option 2, and is not widely used. 
 + 
 +Option 4 offers functionality that is rarely if ever usefuland easily confused with option 3.
  
 For all of the reasons above this RFC proposes to deprecate option 3 and 4 in PHP 8.1 and remove them in PHP 9. For all of the reasons above this RFC proposes to deprecate option 3 and 4 in PHP 8.1 and remove them in PHP 9.
Line 130: Line 155:
 As this is a language change, a 2/3 majority is required. As this is a language change, a 2/3 majority is required.
  
-<doodle title="Deprecate dynamic variable lookup in PHP 8.1 and remove them in PHP 9.0?" auth="ilutov" voteType="single" closed="true">+<doodle title="Deprecate ${} string interpolation in PHP 8.1 and remove them in PHP 9.0?" auth="ilutov" voteType="single" closed="true">
    * Yes    * Yes
    * No    * No
 </doodle> </doodle>
  
rfc/deprecate_dollar_brace_string_interpolation.txt · Last modified: 2022/05/05 08:17 by ilutov