3v4l.org

run code in 300+ PHP versions simultaneously
<?php declare(strict_types=1); class A { } class B extends A { } class C extends B { } class TestA { public function x(C $c):A { return new C(); } } class TestB extends TestA { public function x(B $b):B { return new C(); } } class TestC extends TestB { public function x(A $a): C { return new C(); } } $c = new C(); $testA = new TestA(); $testC = new TestC(); $resultA = $testA->x($c); $resultC = $testC->x($c); var_dump([ 'TestA::x(C): expect C' => get_class($resultA), 'TestC::x(C): expect C' => get_class($resultC), ]); $a = new A(); try { $resultA = $testA->x($a); echo "impossible"; } catch (TypeError $e){ echo "TestA::x(A): expected TypeError\n"; } echo 'TestC::x(A): expect C => '. get_class($testC->x($a));
Output for git.master, git.master_jit, rfc.property-hooks
array(2) { ["TestA::x(C): expect C"]=> string(1) "C" ["TestC::x(C): expect C"]=> string(1) "C" } TestA::x(A): expected TypeError TestC::x(A): expect C => C

This tab shows result from various feature-branches currently under review by the php developers. Contact me to have additional branches featured.

Active branches

Archived branches

Once feature-branches are merged or declined, they are no longer available. Their functionality (when merged) can be viewed from the main output page


preferences:
129.25 ms | 405 KiB | 5 Q