Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

strict type does not work array functions (array_map, array_walk, array_filter) using closures #17283

Closed
proggeler opened this issue Dec 27, 2024 · 2 comments

Comments

@proggeler
Copy link

proggeler commented Dec 27, 2024

Description

The following code:

<?php

declare(strict_types=1); // unfortunately strict types can not be declared on 3v4l.org

$array = [1, 1.2, '1.2.3'];

// var_dump(array_filter($array, function (string $value) { return is_string($value); })); // will not filter
// var_dump(array_map(fn (string $value) => $value, $array)); // all returned values are strings 

array_walk($array, fn (string &$value) => $value); 
var_dump($array);

Resulted in this output:

array(3) {
  [0]=>
  string(1) "1"
  [1]=>
  string(3) "1.2"
  [2]=>
  string(5) "1.2.3"
}

But I expected this output instead:

PHP Fatal error:  Uncaught TypeError: {closure}(): Argument #1 ($a) must be of type string, int given, .....

(as is thrown when call_user_func is used with a closure with a wrong argument type) 

PHP Version

PHP 8.3.6

Operating System

No response

@Girgias
Copy link
Member

Girgias commented Dec 27, 2024

This is expected behaviour. Any function that is "run" by the engine is always in weak mode, which is the case here.

@Girgias Girgias closed this as not planned Won't fix, can't repro, duplicate, stale Dec 27, 2024
@proggeler
Copy link
Author

@Girgias thank you for clarifying.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants