Re: [Discussion] Why can't I do "{$a::class}"?
| From: | Ilija Tovilo | Date: | Sun, 19 May 2024 11:15:12 +0000 |
| Subject: | Re: [Discussion] Why can't I do "{$a::class}"? | ||
| References: | 1 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-123366@lists.php.net to get a copy of this message | ||
Hi Peter
On Sun, May 19, 2024 at 10:30 AM Peter Stalman <sarkedev@gmail.com> wrote:
>
> echo " {A::$static_property} \n"; // doesn't work (unless $static_property is a
> variable)
> echo " {$a::$static_property} \n"; // works
>
> echo " {A::static_method()} \n"; // doesn't work (just text)
> echo " {$a::static_method()} \n"; // works
>
> echo " {A::constant} \n"; // doesn't work
> echo " {$a::constant} \n"; // doesn't work either, but why?
It would be straightforward to allow all expressions that start with a
$ in string interpolation, as I've noticed a couple of years ago
[1]. This restriction seems rather arbitrary.
I think I held off proposing this because I was planning on proposing
a more complete form of string interpolation [2]. However, the
backwards-compatible syntax was largely disliked, so I withdrew the
RFC.
I wasn't particularly fond of introducing more forms of strings (e.g.
$"", f"", etc.) to avoid the BC break, because there are already
plentiful:
* ''
* ""
* `` (yes, these allow interpolation)
* <<<HD ... HD
* <<<'HD' ... HD
Adding at least the two expected ones ($"" and $<<<HD ... HD) would
bump that up to 7 forms.
There were also suggestions to extend strings in a more generic way,
akin to JavaScripts template strings [3], but I didn't have any use
for this myself.
Ilija
[1] https://github.com/php/php-src/compare/master...iluuu1994:php-src:lift-variable-restriction-for-brace-dollar-string-interpolation
[2] https://wiki.php.net/rfc/arbitrary_string_interpolation
[3] https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals