Last active
May 25, 2022 12:51
-
-
Save bagart/ceb8225f152cd13463268090f8ded18f to your computer and use it in GitHub Desktop.
>< of parent/children class
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
https://3v4l.org/WV4jo