CKTIS / PHP
TypeError
Ohlási hodnotu nezodpovedajúcu požadovanému typu argumentu či výsledku. strict_types potlačí väčšinu skalárnych prevodov pri volaní používateľskej funkcie. int naďalej vyhovie float.
Verzia: PHP 8.4; repository runtime, production runtime not attested
Syntax
catch (TypeError $failure) { /* handle invalid type */ }Ohlási hodnotu nezodpovedajúcu požadovanému typu argumentu či výsledku. strict_types potlačí väčšinu skalárnych prevodov pri volaní používateľskej funkcie. int naďalej vyhovie float. TypeError dedí z Error a spĺňa Throwable.
declare(strict_types=1);
function twice(int $n): int{return $n*2;}
try{twice("2");}catch(TypeError $e){echo json_encode([twice(2),$e instanceof Error,$e instanceof Throwable]);}
[4,true,true]
Príklady
<?php
declare(strict_types=1);
function twice(int $n): int{return $n*2;}
try{twice("2");}catch(TypeError $e){echo json_encode([twice(2),$e instanceof Error,$e instanceof Throwable]);}
Výsledok
[4,true,true]