当前位置:网站首页>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 deprecated
This 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)}");//> bar
The 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
边栏推荐
猜你喜欢
随机推荐
npm--package.json---require
---静态页面---
PHP 给图片添加全图水印
PHP基金会三月新闻公告发布
批量替换文件字体,简体->繁体
Common methods of js array deduplication
客户评分控件
PHP8.2将会有哪些新东西?
每日五道面试题总结 22/7/20
[vite] Failed to parse source for import analysis because the content contains invalid JS syntax.
多线程(实现多线程、线程同步、生产者消费者)
由中序遍历和前序遍历得到后序遍历(树的遍历)
ES6三点运算符、数组方法、字符串扩展方法
Circular linked list---------Joseph problem
ES6迭代器解释举例
canvas--饼状图
这些JS题面试时一定要答对!
---static page---
clock tick marks
轮播图详解(完整代码在最后)