While working on the following code I noticed that declare(strict_types=1)
has no effect on the arguments of a callback function through 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
?>
I'm expecting php to throw an exception instead of 444
because [meat] value in $myArr
is not integer!
apparently php ignores the fact that [meat] in $myArr
is float
for some reason! instead of throwing an exception just like what happened with myFunc()
.
Is this a normal php behavior or am I missing something?.