TypeError

(PHP 7, PHP 8)

Introduction

There are three scenarios where a TypeError may be thrown:

  • The argument type being passed to a function does not match its corresponding declared parameter type.
  • A value being returned from a function does not match the declared function return type.
  • An invalid number of arguments are passed to a built-in PHP function (strict mode only).

Class synopsis

TypeError extends Error {
/* Inherited properties */
protected string $message;
protected int $code;
protected string $file;
protected int $line;
/* Inherited methods */
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