当前位置:网站首页>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

边栏推荐
猜你喜欢
随机推荐
js的“类数组”及“类数组转数组”
STM32 CAN过滤器
微信小程序全局组件的定义
Small program van-cell line wrapping can be left-aligned
第一次手撕代码,如何解出全排列问题
TCP通信程序
14.JS语句和注释,变量和数据类型
[symfony/mailer]一个优雅易用的发送邮件类库
PHP基金会三月新闻公告发布
URL URL
微信小程序云开发之券码领取,怎么防止用户领取到相同的数据?
[phpunit/php-timer]一个用于代码执行时间的计时器
解决uni-app 打包H5网站 下载图片问题
Living to detect the Adaptive Normalized Representation Learning for GeneralizableFace Anti - Spoofing reading notes
微信小程序云开发如何将页面生成为pdf?
ES6介绍+定义变量+不同情况下箭头函数的this指向
PHP8.2中字符串变量解析的新用法
uniapp | 开发中遇到的兼容性问题(待续)
[sebastian/diff]一个比较两段文本的历史变化扩展库
IO streams, byte stream and byte stream buffer









