当前位置:网站首页>New usage of string variable parsing in PHP8.2
New usage of string variable parsing in PHP8.2
2022-08-02 03:53:00 【phpreturn】
PHP allows variables to be embedded in double-quoted strings, previously supported in these ways:
- Embed variables directly (
"$var") - Use curly braces around variables (
"{$var}") - Use curly braces after $ (
"${var}") - Variable variables (
"${expr}"), Variable Variable Reference
The 1st and 2nd usages have their pros and cons.But the 3rd usage and the 4th are easily confused due to overlapping syntax.The third usage is not as strict as the first two, and will be confused with the fourth usage, (refer to the variable variable documentation), and is rarely used in string variable parsing.
Uses 3 and 4 will be deprecated in PHP 8.2 and removed in PHP 9.
var_dump("${foo}");// DEPRECATED: Using ${} in strings is deprecatedvar_dump("${(foo)}");// DEPRECATED: ${} (variable variable) in strings is deprecatedThis leaves only the first two usages, the first one is straightforward and the second one is powerful and supports various types of usages (array writing, etc.)
Current Version
In the current version, the purpose of the first three usages is the same, which is to embed simple variables into strings.It's just that they don't work the same.
Simple concatenation of variables
The first three usages all support basic variable concatenation.
$foo = 'foo' ;var_dump("$foo");var_dump("{$foo}");var_dump("${foo}");Concatenate the specified array index
The first three usages also support splicing the specified array index, but the syntax is not consistent.
$foo = ['bar' => 'bar'];var_dump("$foo[bar]");var_dump("{$foo['bar']}");var_dump("${foo['bar']}");It should be noted here that the 2nd and 3rd usages support any index name, but in the 1st usage, the index cannot contain spaces and other special symbols, only simple strings (such as $foo[a b] ).
Mosaic object properties
The first two methods support accessing object properties.
$foo = (object) ['bar' => 'bar'];var_dump("$foo->bar");var_dump("{$foo->bar}");Mosaic object method
Only the second method supports accessing object methods.
class Foo {public function bar() {return 'bar';}}$foo = new Foo();var_dump("{$foo->bar()}");Composite call concatenation
Only the second method supports complex calling methods.
class Bar {public function baz() {return 'baz';}}$foo = ['bar' => new Bar()];var_dump("{$foo['bar']->baz()}");Fourth usage
PHP has a feature called mutable variables, which allows variables to be obtained by name, and this name can be stored in a variable.
$foo = 'Hello world!';$bar = 'foo';var_dump(${$bar});This is true even in string variable parsing:
$foo = 'world!';$bar = 'foo';var_dump("Hello ${$bar}");You will notice that this syntax conflicts with Section 3.If the syntax between the curly braces is inconsistent with usage 3, PHP performs variable resolution with completely different semantics.
const foo = 'bar';$foo = 'foo';$bar = 'bar';var_dump("${foo}");//> foovar_dump("${(foo)}");//> barThe curly braces switch from usage 3 to usage 4 because parentheses are not allowed in usage 3.This means that foo is no longer interpreted as a variable but a constant, then usage 4 will try to find the local variable by that constant's name.This is very winding.While this representation might be useful (the author can't imagine how), it's worth noting that they would work just as well with syntax 2, which is more intuitive.
Conclusion
Usage 1 is easy to use and very common.
Usage 2 is powerful and very common.
Usage 3 only implements part of the functions of the first two usages, and is not used much.
Usage 4 has few features and is easily confused with option 3.
In summary, both usages will be deprecated in PHP 8.2 and removed in PHP 9.
Affects
Analysis of the top 1000 packagegist repositories yielded 267 uses of option 3 and 0 uses of option 4.
How to migrate code
Migrating from usage 3 to usage 2 is as simple as moving the $ sign.
"${foo}" => "{$foo}""${foo[expr]}" => "{$foo[expr]}"Migrating from usage 4 to usage 2 is also as simple as putting curly braces around the difference.
"${foo->bar}" => "{${foo->bar}}"The only trouble is how to correctly distinguish whether this code wants to use usage 3 or usage 4, (this modification is to solve this problem).But found in statistics, usage 4 should not exist.
Invariant usage
- Variable variable outside of string
$foo = ${'bar'}; - Variable variable for usage 1
echo "$$foo";
Comparison with other languages
Many other languages have usage 3 syntax, especially in template parsing in Bash and JavaScript.But their behavior is completely different from PHP.In PHP this syntax means mutable variables.In JavaScript, he supports arbitrary forms of usage2.Currently usage 3 and usage 4 are of limited use and can be confusing for developers who have learned other languages.Their behavior is completely different.
Future Development Direction
Usages 1 and 2 are still not perfect, they only allow simple variable parsing.In the future, we might discuss that any expression can be parsed in a string, such as functions like this.
var_dump("{$:func()}")Before that, it was necessary to remove some confusing usages.
Reference article: PHP: rfc:deprecate_dollar_brace_string_interpolation

边栏推荐
猜你喜欢
随机推荐
微信小程序云开发-证件照的实现
正则笔记(1)- 正则表达式字符匹配攻略
阿里云服务器如何使用admin账户登录
Dom实现input的焦点触发
5.20今日学习
mysql阶段总结
微信小程序云开发之券码领取,怎么防止用户领取到相同的数据?
PHP 发起支付宝支付时 订单信息乱码解决
1.6一些今日学习
[league/climate]一个功能健全的命令行功能操作库
__dirname
1.8今日学习
项目中遇到的问题
Query the indexes of all tables in the database and parse them into sql
简单黑马头条项目
display,visibility,opacity
vue3 访问数据库中的数据
PHP基金会三月新闻公告发布
百度定位js API
正则笔记(2)- 正则表达式位置匹配攻略









