Hi internals

I've been thinking about ways to improve string interpolation. String
interpolation in PHP is currently pretty limited to say the least.
What's worse is we have several ways to do the same thing and a few of
them being quite inconsistent and confusing.

We have these syntaxes:

1. Directly embedding variables: "$foo"
2. Braces outside the variable "{$foo}"
3. Braces after the dollar sign: "${foo}"
4. Dynamic variable lookup: "${expr}"

The difference between 3 and 4 is extra confusing. Passing a simple
variable name without a dollar inside the braces (and optionally an
offset access) will embed the given variable in the string. When
passing a more complex expression ${} will behave completely
differently and perform a dynamic variable lookup.
https://3v4l.org/uqcjf

Let's look at some different examples.

Simple local variables work well, although it is questionable why we
have 4 ways to do the same thing.

1. "$foo"
2. "{$foo}"
3. "${foo}"
4. "${'foo'}"

Accessing offsets is supported for syntax 1, 2 and 3. String quoting
in the offset expression is inconsistent.

1. "$foo[bar]"
2. "{$foo['bar']}"
3. "${foo['bar']}"

Accessing properties is supported for syntax 1 and 2.

1. "$foo->bar"
2. "{$foo->bar}"

Nested property fetches or offset accesses only work with syntax 2.

1. "$foo[bar][baz]" // [baz] is not part of the expression, equivalent
to "{$foo['bar']}[baz]"
2. "{$foo['bar']['baz']}"
3. "${foo['bar']['baz']}" // Syntax error

Calling methods is only supported for syntax 2.

1. "$foo->bar()" // Treats ->bar as a property access, equivalent to
"{$foo->bar}()"
2. "{$foo->bar()}"

Arbitrary expressions work for none of the syntaxes.

2.
    * "{$foo + 2}" // Syntax error
    * "{Foo::bar()}" // Interpreted as string

The most functional of these syntaxes is 2 but even here only a small
subset of expressions are accepted. The distinction between syntax 3
and 4 is very confusing and we should probably deprecate and remove
syntax 3 (as it does pretty much the same as syntax 2). Sadly, the
only syntax that accepts arbitrary expressions (4) is also the least
useful.

As to allowing arbitrary string interpolation, we have three options:

1. Deprecate syntax 4, remove it, and change its functionality in the future
2. Use some new syntax (e.g. "\(1 + 2)", "\{1 + 2}", "$(1 + 2)", etc)
with the given BC break
3. Do nothing

Adding new syntax will break many regular expressions and embedded
jQuery code, although they are easily fixed by escaping the \ or $.
Deprecating, removing and then changing ${} would take many years. I
don't know which of these is the better approach.

Any thoughts or different ideas?

Ilija

-- 
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: https://www.php.net/unsub.php

Reply via email to