TypePHP 编译器 https://swoole.com/aot/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

44 lines
929 B

--TEST--
Symfony style dynamic instanceof against pipe-separated type string
--FILE--
<?php
interface UserInterface {}
class AdminUser implements UserInterface {}
class GuestUser {}
function resolveUser(object $user, ?string $type): array
{
if (null === $type || $user instanceof ($type)) {
return [$user::class];
}
$types = explode('|', $type);
foreach ($types as $candidate) {
if ($user instanceof $candidate) {
return [$user::class, $candidate];
}
}
return [];
}
function main(): void
{
var_dump(resolveUser(new AdminUser(), UserInterface::class));
var_dump(resolveUser(new AdminUser(), GuestUser::class.'|'.UserInterface::class));
var_dump(resolveUser(new GuestUser(), UserInterface::class));
}
?>
--EXPECT--
array(1) {
[0]=>
string(9) "AdminUser"
}
array(2) {
[0]=>
string(9) "AdminUser"
[1]=>
string(13) "UserInterface"
}
array(0) {
}