<?php
class C {
public function getThis(): self {
return $this;
}
public function getThat(): self {
// Fails.
return new stdClass;
}
public function takeThis(self $c) {}
}
var_dump((new C)->getThis());
var_dump((new C)->takeThis(new C));
var_dump((new C)->getThat());
object(C)#1 (0) {
}
NULL
Fatal error: Uncaught TypeError: C::getThat(): Return value must be of type C, stdClass returned in /in/g0p5l:10
Stack trace:
#0 /in/g0p5l(18): C->getThat()
#1 {main}
thrown in /in/g0p5l on line 10
Process exited with code 255.
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
object(C)#1 (0) {
}
NULL
Fatal error: Uncaught TypeError: C::getThat(): Return value must be of type C, stdClass returned in /in/g0p5l:10
Stack trace:
#0 /in/g0p5l(18): C->getThat()
#1 {main}
thrown in /in/g0p5l on line 10
Process exited with code 255.
object(C)#1 (0) {
}
NULL
Fatal error: Uncaught TypeError: Return value of C::getThat() must be an instance of C, instance of stdClass returned in /in/g0p5l:10
Stack trace:
#0 /in/g0p5l(18): C->getThat()
#1 {main}
thrown in /in/g0p5l on line 10
Process exited with code 255.
Parse error: syntax error, unexpected ':', expecting ';' or '{' in /in/g0p5l on line 4
Process exited with code 255.
Output for 5.0.0 - 5.0.5
Parse error: parse error, unexpected ':', expecting '{' or ';' in /in/g0p5l on line 4
Process exited with code 255.
Output for 4.4.2 - 4.4.9
Parse error: syntax error, unexpected T_STRING, expecting T_OLD_FUNCTION or T_FUNCTION or T_VAR or '}' in /in/g0p5l on line 4
Process exited with code 255.
Parse error: parse error, unexpected T_STRING, expecting T_OLD_FUNCTION or T_FUNCTION or T_VAR or '}' in /in/g0p5l on line 4
Process exited with code 255.
Output for 4.3.2 - 4.3.4
Parse error: parse error, expecting `T_OLD_FUNCTION' or `T_FUNCTION' or `T_VAR' or `'}'' in /in/g0p5l on line 4
Process exited with code 255.