TypeError
(PHP 7, PHP 8)
简介
会抛出TypeError 的情况:
- 为类属性设置的值与该属性申明的类型不匹配。
- 传递给函数的参数类型与函数预期声明的参数类型不匹配。
- 函数返回的值与声明的函数返回类型不匹配
类摘要
更新日志
版本 | 说明 |
---|---|
7.1.0 | 当在严格模式下向内置 PHP 函数传递无效数量的参数时,不再抛出 TypeError。 相反,会抛出 ArgumentCountError |
add a note
User Contributed Notes 2 notes
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 !";
}
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();
}