PHP Reference ยท Exceptions and Errors
The PHP TypeError class
Reference for PHP TypeError: when it is thrown for wrong argument, return and property types, unsupported operands and strict_types, and how to handle it.
TypeError is an Error that PHP throws when a value has the wrong type for where it is used. The common cases are an argument that does not match a parameter type, a return value that does not match the declared return type, a typed property assigned the wrong kind of value, and operators applied to operands they do not support.
Whether a mismatch is an error depends on the mode. In the default weak mode PHP first tries to convert scalars ("5" can become 5 for an int parameter) and only throws when conversion is impossible. With declare(strict_types=1) in the calling file, scalar conversion is off and any mismatch throws, except that an int is still accepted for a float.
function double(int $n): int { return $n * 2; } double("abc"); // TypeError
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with fgets(STDIN)), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with fgets(STDIN)), it gets the next line from this box.
Syntax
class TypeError extends Error {}
It adds no methods; use the ones inherited from Error: getMessage(), getCode(), getFile(), getLine(), getTrace(), getPrevious().
| Situation | Message pattern |
|---|---|
| User function argument | f(): Argument #1 ($n) must be of type int, string given, called in ... on line N |
| Return type | f(): Return value must be of type string, array returned |
| Typed property | Cannot assign string to property Box::$size of type int |
| Operator | Unsupported operand types: array + int |
| Built-in function | str_repeat(): Argument #2 ($times) must be of type int, string given |
Subclass: ArgumentCountError is thrown when too few arguments are passed.
Usage notes
- Fix the cause. A
TypeErrorusually means a bug or unchecked input. Convert or validate at the boundary (withfilter_var()or casts) instead of catching it everywhere. - Weak mode converts. A numeric string passes for
intorfloat, anintfor astring, and any scalar forbool. A non-numeric string, an array or an object does not. - Strict mode is per file and applies to calls made from that file, not to the file where the function is declared. See
strict_types. nullis not accepted by a non-nullable user function parameter. For built-in functions, passingnullto a non-nullable scalar parameter is deprecated since PHP 8.1 and still works for now.- Union types (PHP 8.0+) widen what is allowed; see Union types.
- Arithmetic on arrays throws
TypeError("Unsupported operand types"). See the notes on arithmetic operators for how non-numeric strings behave. - Catch it at boundaries, such as when parsing external data, to report a bad request, not to cover up wrong code.
- Typed properties reject wrong types on assignment even in weak mode if conversion is impossible; uninitialized typed properties throw an
Errorwhen read.