5

在处理以下代码时,我注意到declare(strict_types=1)它对回调函数的参数没有影响array_walk()

<?php
declare(strict_types=1);

function myCallBack(int $value, $key) {

    echo $value;
}

function myFunc(int $value, $key) {
    echo $value;
}

$myArr = array("eggs" => 4, "Butter" => 4, "meat" => 4.5);

echo 'myCallBack..';
array_walk($myArr, 'myCallBack'); // Output: 444 

echo ' <br />myFunc..';
myFunc(4.2, 'eggs'); // Output:Fatal error: Uncaught TypeError: Argument 1 passed to myFunc() must be of the type integer

?>

我期待 php 抛出异常,而不是444因为 [meat] 中的值$myArr不是整数!

显然 php 忽略了 [meat] in$myArrfloat出于某种原因的事实!而不是像myFunc().

这是正常的 php 行为还是我错过了什么?

4

0 回答 0