TypeError

(PHP 7, PHP 8)

Einführung

Ein TypeError kann geworfen werden, wenn:

  • der Wert, der für eine Klasseneigenschaft festgelegt wird, nicht dem deklarierten Typ der Eigenschaft entspricht.
  • der Typ eines Arguments, das an eine Funktion übergeben wird, nicht mit dem deklarierten Typ des entsprechenden Parameters übereinstimmt.
  • ein von einer Funktion zurückgegebener Wert nicht dem deklarierten Rückgabetyp der Funktion entspricht.

Klassenbeschreibung

class TypeError extends Error {
/* Geerbte Eigenschaften */
protected string $message = "";
private string $string = "";
protected int $code;
protected string $file = "";
protected int $line;
private array $trace = [];
private ?Throwable $previous = null;
/* Geerbte Methoden */
public Error::__construct(string $message = "", int $code = 0, ?Throwable $previous = null)
final public Error::getMessage(): string
final public Error::getPrevious(): ?Throwable
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
private Error::__clone(): void
}

Changelog

Version Beschreibung
7.1.0 Wenn im Modus strict eine ungültige Anzahl von Argumenten an eine interne PHP-Funktion übergeben wird, wird kein TypeError mehr geworfen; stattdessen wird ein ArgumentCountError geworfen.
add a note

User Contributed Notes 2 notes

up
4
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
-14
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