略微加速

PHP官方手册 - 互联网笔记

PHP - Manual: TypeError

2024-04-28

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;
/* 继承的方法 */
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
}

更新日志

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

User Contributed Notes 2 notes

up
0
celsowmbr at outlook dot com
3 years ago
An example:

<?php

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

try {
   
test('ss');
}catch(
TypeError $e){
    echo
"Error !";
}
up
-4
andrian dot test dot job at gmail dot com
2 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();

}

官方地址:https://www.php.net/manual/en/class.typeerror.php

北京半月雨文化科技有限公司.版权所有 京ICP备12026184号-3