TypeError

(PHP 7, PHP 8)

简介

会抛出TypeError 的情况:

  • 为类属性设置的值与该属性申明的类型不匹配。
  • 传递给函数的参数类型与函数预期声明的参数类型不匹配。
  • 函数返回的值与声明的函数返回类型不匹配

类摘要

class TypeError extends Error {
/* 继承的属性 */
protected string $message = "";
private string $string = "";
protected int $code;
protected string $file = "";
protected int $line;
private array $trace = [];
private ?Throwable $previous = null;
/* 继承的方法 */
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
}

更新日志

版本 说明
7.1.0 当在严格模式下向内置 PHP 函数传递无效数量的参数时,不再抛出 TypeError。 相反,会抛出 ArgumentCountError
add a note

User Contributed Notes 1 note

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

<?php

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

try {
   
test('ss');
}catch(
TypeError $e){
    echo
"Error !";
}
To Top