Strict_types does not affect array_walk

When working on the following code, I noticed that declare (strict_types = 1) does not affect the arguments to the callback function via 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 expect php to throw an exception instead of 444 because the [meat] value in $ myArr is not an integer!

Apparently php ignores the fact that [meat] in $ myArr is a float for some reason! instead of throwing an exception, just like with myFunc ().

Is this normal php behavior or am i missing something?

+4
source share

Source: https://habr.com/ru/post/1690283/


All Articles