TypeError

(PHP 7, PHP 8)

Introduction

A TypeError may be thrown when:

  • The value being set for a class property does not match the property's corresponding declared type.
  • 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.

Class synopsis

class TypeError extends Error {
/* Inherited properties */
protected string $message = "";
private string $string = "";
protected int $code;
protected string $file = "";
protected int $line;
private array $trace = [];
private ?Throwable $previous = null;
/* Inherited methods */
public Error::__construct(string $message = "", int $code = 0, ?Throwable $previous = null)
final public Error::getMessage(): string
final public Error::getCode(): int
final public Error::getFile(): string
final public Error::getLine(): int
final public Error::getTrace(): array
private Error::__clone(): void
}

Changelog

Version Description
7.1.0 A TypeError is no longer thrown when an invalid number of arguments are passed to a built-in PHP function in strict mode. Instead, an ArgumentCountError is raised.
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