今天看到有人在群里提到,PHP7新特性--返回值类型声明,索性就去看了一下,大概是这样的写的,代码贴上来看一下。
代码语言:javascript复制<?php
function arraysSum(array ...$arrays): array
{
return array_map(function(array $array): int {
return array_sum($array);
}, $arrays);
}
print_r(arraysSum([1,2,3], [4,5,6], [7,8,9]));
die;
如果array_map() 返回值类型不是int 就会类似的抛错。
代码语言:javascript复制//Fatal error: Uncaught TypeError: Return value of {closure}() must be of the type integer, string returned in E:hzc2_localwwwwebindex.php:7 Stack trace: #0 [internal function]: {closure}(Array) #1 E:hzc2_localwwwwebindex.php(9): array_map(Object(Closure), Array) #2 E:hzc2_localwwwwebindex.php(15): arraysSum(Array, Array, Array) #3 {main} thrown in E:hzc2_localwwwwebindex.php on line 7