<?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.3.5
Warning: PHP Startup: Unable to load dynamic library 'sodium.so' (tried: /usr/lib/php/8.3.5/modules/sodium.so (libsodium.so.23: cannot open shared object file: No such file or directory), /usr/lib/php/8.3.5/modules/sodium.so.so (/usr/lib/php/8.3.5/modules/sodium.so.so: cannot open shared object file: No such file or directory)) in Unknown on line 0
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.