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.33, 8.2.10 - 8.2.29, 8.3.0 - 8.3.27, 8.4.1 - 8.4.14
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:
103.09 ms | 412 KiB | 6 Q