Base de conhecimentoCentro de Exames
PT · PTPortuguês (Portugal)
Escolher idioma13

CKTIS / PHP

TypeError

Indica um valor incompatível com o tipo exigido de argumento ou retorno. strict_types evita a maioria das conversões escalares ao chamar funções próprias. int continua válido para float.

Versão: PHP 8.4; repository runtime, production runtime not attested

Sintaxe

catch (TypeError $failure) { /* handle invalid type */ }

Indica um valor incompatível com o tipo exigido de argumento ou retorno. strict_types evita a maioria das conversões escalares ao chamar funções próprias. int continua válido para float. TypeError herda de Error e é 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]

Exemplos

<?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]

Fontes