<?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;
}
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.