TypeError

(PHP 7, PHP 8)

Introduction

Il y a trois scénario où une TypeError peut être lancée :

  • Le type de l'argument qui est passé à la fonction ne correspond pas à la déclaration du type du paramètre correspondant.
  • Une valeur qui est retourné par une fonction ne correspond pas au type de retour déclaré par la fonction.
  • Un nombre invalide d'arguments sont fournis à une fonction intégrée de PHP (mode stricte uniquement).

Synopsis de la classe

TypeError extends Error {
/* Propriétés héritées */
protected string $message;
protected int $code;
protected string $file;
protected int $line;
/* Méthodes héritées */
final public Error::getMessage(): string
final public Error::getPrevious(): Throwable|null
final public Error::getCode(): int
final public Error::getFile(): string
final public Error::getLine(): int
final public Error::getTrace(): array
final public Error::getTraceAsString(): string
public Error::__toString(): string
final private Error::__clone(): void
}
add a note add a note

User Contributed Notes 2 notes

up
0
celsowmbr at outlook dot com
5 years ago
An example:

<?php

function test($x):int {
    return
$x;
}

try {
   
test('ss');
}catch(
TypeError $e){
    echo
"Error !";
}
up
-1
andrian dot test dot job at gmail dot com
4 years ago
declare(strict_types=1); //if without this line the result is different

$a = [1,2=>[3,4]];

try{

    count($a, COUNT_RECURSIVE, 'toto and blabla');

}catch(TypeError $e){

    echo $e->getMessage();

}
To Top