Fat*_*ror 5 php callback array-walk
在处理以下代码时,我注意到这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
?>
Run Code Online (Sandbox Code Playgroud)
我期待 php 抛出异常而不是444
因为 [meat] 值$myArr
不是整数!
显然 php 忽略了 [meat] in$myArr
是float
出于某种原因的事实!而不是像myFunc()
.
这是正常的 php 行为还是我遗漏了什么?
归档时间: |
|
查看次数: |
324 次 |
最近记录: |