3v4l.org

run code in 300+ PHP versions simultaneously
<?php class Foo implements Stringable { const HOGE = 'hoge'; public function __toString(): string { return 'HOGE'; } } function f(Stringable $name): void { var_dump(Foo::{(string)$name}); var_dump(Foo::{$name}); } f(new Foo);
Output for 8.3.0 - 8.3.26, 8.4.1 - 8.4.13
string(4) "hoge" Fatal error: Uncaught TypeError: Cannot use value of type object as class constant name in /in/WZTCC:14 Stack trace: #0 /in/WZTCC(17): f(Object(Foo)) #1 {main} thrown in /in/WZTCC on line 14
Process exited with code 255.
Output for 8.2.0 - 8.2.29
Parse error: syntax error, unexpected token ")", expecting "(" in /in/WZTCC on line 13
Process exited with code 255.

preferences:
53.58 ms | 407 KiB | 5 Q