CKTIS / PHP
TypeError
Indica un valor incompatible con el tipo exigido de argumento o retorno. strict_types evita la mayoría de conversiones escalares al llamar funciones propias. int sigue siendo válido para float.
Versión: PHP 8.4; repository runtime, production runtime not attested
Sintaxis
catch (TypeError $failure) { /* handle invalid type */ }Indica un valor incompatible con el tipo exigido de argumento o retorno. strict_types evita la mayoría de conversiones escalares al llamar funciones propias. int sigue siendo válido para float. TypeError hereda de Error y es 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]
Ejemplos
<?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]);}
Resultado
[4,true,true]