Код: Выделить всё
function test_type(mixed $value, string $type): bool
{
try {
eval('(fn('.$type.' $a)=>true)($value);');
return true;
}
catch (Throwable $e) {
return false;
}
}
test_type(null, "int") ; // returns false.
test_type(null, "?int") ; // return true.
test_type("hello, world", "bool") ; // returns true. I want it to return false
test_type(new DateTime, "DateTimeInterface") ; // return true.
test_type(new DateTime, "null|int|DateTime|DateTimeImmutable") ; // return true
- eval
- автоматическое преобразование типов (как в test_type("hello, world", "bool") => true )
Подробнее здесь: https://stackoverflow.com/questions/791 ... -in-string
Мобильная версия