3v4l.org

run code in 300+ PHP versions simultaneously
<?php class a { public function test(): self|string { return new \stdClass; } }; $b = new class { public function test(): self|string { return new \stdClass; } }; try { (new a)->test(); } catch (\Throwable $e) { echo $e->getMessage().PHP_EOL; } try { $b->test(); } catch (\Throwable $e) { echo $e->getMessage().PHP_EOL; }
Output for 8.0.2, 8.1.23 - 8.1.34, 8.2.10 - 8.2.30, 8.3.0 - 8.3.30, 8.4.1 - 8.4.18, 8.5.0 - 8.5.3
a::test(): Return value must be of type a|string, stdClass returned class@anonymous::test(): Return value must be of type class@anonymous|string, stdClass returned
Output for 8.0.0 - 8.0.1
a::test(): Return value must be of type a|string, stdClass returned class@anonymous::test(): Return value must be of type class@anonymous, stdClass returned
Output for 7.3.0 - 7.3.27, 7.4.0 - 7.4.15
Parse error: syntax error, unexpected '|', expecting ';' or '{' in /in/e83WV on line 4
Process exited with code 255.

preferences:
86.36 ms | 1068 KiB | 4 Q