3v4l.org

run code in 300+ PHP versions simultaneously
<?php class A1{} class A2 extends A1{} class C1{ function test() : A1{ return new A1; } } class C2 extends C1{ function test() : A2{ return new A2; } } $x = new C1; var_dump($x->test()); $x = new C2; var_dump($x->test());
Output for 7.4.0 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.34, 8.2.0 - 8.2.30, 8.3.0 - 8.3.30, 8.4.1 - 8.4.18, 8.5.0 - 8.5.3
object(A1)#2 (0) { } object(A2)#1 (0) { }
Output for 7.0.0 - 7.0.33, 7.1.0 - 7.1.33, 7.2.0 - 7.2.33, 7.3.0 - 7.3.33
Fatal error: Declaration of C2::test(): A2 must be compatible with C1::test(): A1 in /in/VT4iO on line 17
Process exited with code 255.
Output for 5.4.0 - 5.4.45, 5.5.0 - 5.5.38, 5.6.0 - 5.6.40
Parse error: syntax error, unexpected ':', expecting ';' or '{' in /in/VT4iO on line 8
Process exited with code 255.

preferences:
98.53 ms | 2304 KiB | 4 Q