PHP why is array_filter ignoring strict_types?

54 Views Asked by At

per 3v4l: https://3v4l.org/qeU8s , In every version of PHP between 7.0.0 and 8.2.9, why is this code

<?php
declare(strict_types=1);

$arr = ["1"];
$filtered = array_filter($arr, function (int $i) {
    var_dump(["func_get_args" => func_get_args()]);
    return true;
});
var_dump($arr);

outputting

array(1) {
  ["func_get_args"]=>
  array(1) {
    [0]=>
    int(1)
  }
}
array(1) {
  [0]=>
  string(1) "1"
}

? I expected

    Fatal error: Uncaught TypeError: {closure}(): Argument #1 ($i) must be of type int, string given in /in/Gf4Pq:5
    Stack trace:
    #0 [internal function]: {closure}('1')
    #1 /in/Gf4Pq(5): array_filter(Array, Object(Closure))
    #2 {main}
      thrown in /in/Gf4Pq on line 5

    Process exited with code 255.

and why is this code

<?php
declare(strict_types=1);

$arr = ["1", "2 strings"];
$filtered = array_filter($arr, function (int $i) {
    var_dump(["func_get_args" => func_get_args()]);
    return true;
});
var_dump($arr);

outputting

    array(1) {
      ["func_get_args"]=>
      array(1) {
        [0]=>
        int(1)
      }
    }

    Fatal error: Uncaught TypeError: {closure}(): Argument #1 ($i) must be of type int, string given in /in/IWcRq:5
    Stack trace:
    #0 [internal function]: {closure}('2 strings')
    #1 /in/IWcRq(5): array_filter(Array, Object(Closure))
    #2 {main}
      thrown in /in/IWcRq on line 5

    Process exited with code 255.

? I expected just the last 8 lines of that.

Seems array_filter ignores strict_types, why is array_filter ignoring strict_types?

0

There are 0 best solutions below