Req #81583 [Com]: Accessing class constants on objects in string interpolation
| From: | janettabloomquist at gmail dot com | Date: | Sat, 31 Dec 2022 09:34:28 +0000 |
| Subject: | Req #81583 [Com]: Accessing class constants on objects in string interpolation | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-243301@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=81583&edit=1
ID: 81583
Comment by: janettabloomquist at gmail dot com
Reported by: php at crmteam dot cytainment dot de
Summary: Accessing class constants on objects in string
interpolation
Status: Open
Type: Feature/Change Request
Package: *General Issues
Operating System: Debian 10
PHP Version: 8.0.12
Block user comment: N
Private report: N
New Comment:
String Interpolation is a process in which the placeholder characters are replaced with the
variables (or strings in this case). This page describes the feature in PHP. It appears that
constants are not replaced during string interpolation in PHP, so the only way to get the behavior
you want is to use the concatenation that Artefacto suggested.
(https://www.skywardalpine.com/)github.com
Previous Comments:
------------------------------------------------------------------------
[2022-12-06 08:16:55] Aleta3215acias at gmail dot com
Because "constants inside quotes are not printed". The correct form is:
echo "This is a constant: " . CONSTANT;
The dot is the concatenation operator.
(https://www.dgcustomerfirst.win/)github.com
------------------------------------------------------------------------
[2021-11-01 13:17:27] nikic@php.net
This is not really specific to ::class, applies to $a::XYZ generally. In any case, this is clearly a
feature request.
------------------------------------------------------------------------
[2021-11-01 10:03:59] php at crmteam dot cytainment dot de
Description:
------------
The introduced feature ::class on objects is throwing a syntax error when being used in string
concatenation using curly braces.
Error:
PHP Parse error: syntax error, unexpected token "}", expecting "->" or
"?->" or "{" or "[" in php shell code on line 1
Test script:
---------------
class Test {};
$a = new Test();
echo "Test " . $a::class;
echo "Test {$a::class}";
Expected result:
----------------
In my opinion the lines 3 and 4 should output the same result "Test Test"
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=81583&edit=1