Let's say I have
class X {
}
class Y {
}
function foo(X|Y $param) {
}
class Z extends Y {
}
class U {
}
Now, how could one verify that passing an instance of Z to foo works but an instance of U will not? Manually, it'd look like...
$r = new \ReflectionFunction('foo');
$found = FALSE;
foreach ($r->getParameters()[0]->getType()->getTypes() as $type) {
$name = $type->getName();
if ($name && is_subclass_of('\Z', $name)) {
$found = TRUE;
}
}
This is ugly to say the least and I am not even sure whether the non-emptiness check of $name is necessary.
Is there perhaps some "type matches" functionality in PHP 8?