CKTIS / PHP
TypeError
Segnala un valore incompatibile con il tipo richiesto di argomento o ritorno. strict_types elimina la maggior parte delle conversioni scalari nelle chiamate a funzioni utente. int resta ammesso per float.
Versione: PHP 8.4; repository runtime, production runtime not attested
Sintassi
catch (TypeError $failure) { /* handle invalid type */ }Segnala un valore incompatibile con il tipo richiesto di argomento o ritorno. strict_types elimina la maggior parte delle conversioni scalari nelle chiamate a funzioni utente. int resta ammesso per float. TypeError deriva da Error ed è 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]
Esempi
<?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]);}
Risultato
[4,true,true]