<?php // throwable_error.php try { function takesInt(int $x) { return true; } takesInt('not an int'); } catch (Exception $e) { echo 'Exception caught<br>'; } catch (Throwable $t) { echo 'Throwable caught<br>'; } finally { echo 'In finally<br>'; }
$result = takesInt($b); echo "Returned:<br>"; var_dump($result); }); safeRun("Passing a string", function () { $s = "1"; $result = takesInt($s); echo "Returned:<br>"; var_dump($result); }); safeRun("Passing a string which in no way can be considered an int", function () { $s = "not an integer"; $result = takesInt($s); echo "Returned:<br>"; var_dump($result); }); safeRun("Passing a PHP object", function () { $d = new DateTime(); $result = takesInt($d); echo "Returned:<br>"; var_dump($result); }); class C { } safeRun("Passing a bespoke object", function () { $o = new C(); $result = takesInt($o); echo "Returned:<br>"; var_dump($result); });
<?php // intObject.php require __DIR__ . "/safeRun.php"; class int { } function takesInt(int $i) { return $i; } safeRun("Passing an int (object)", function () { $i = new int(); $result = takesInt($i); echo "Returned:<br>"; var_dump($result); });