<!-- wp:paragraph -->
<p>今天看到有人在群里提到</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>PHP7新特性--返回值类型声明</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>索性就去看了一下。</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>大概是这样的写的,代码贴上来看一下。</p>
<!-- /wp:paragraph -->

<!-- wp:code -->

<?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;

<!-- /wp:code -->

<!-- wp:image {"linkDestination":"custom"} -->
<figure class="wp-block-image">运行结果</figure>
<!-- /wp:image -->

<!-- wp:paragraph -->
<p>如果array_map() 返回值类型不是int 就会类似的抛错。</p>
<!-- /wp:paragraph -->

<!-- wp:code -->

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
<!-- /wp:code -->

关注友儿不迷路

Last modification:December 28th, 2019 at 08:52 pm
如果觉得我的文章对你有用,请随意赞赏