php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #74750 strict_types not used for builtin callbacks (eg array_map)
Submitted: 2017-06-12 16:32 UTC Modified: 2017-06-13 11:53 UTC
Votes:2
Avg. Score:3.5 ± 0.5
Reproduced:2 of 2 (100.0%)
Same Version:0 (0.0%)
Same OS:1 (50.0%)
From: fredemmott@php.net Assigned:
Status: Open Package: Scripting Engine problem
PHP Version: 7.1.6 OS: Debian Jessie
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: fredemmott@php.net
New email:
PHP Version: OS:

 

 [2017-06-12 16:32 UTC] fredemmott@php.net
Description:
------------
In a strict file: 
 - declare a function with typed parameters
 - call a builtin in a way that types don't match (eg if your function takes a string, array_map over ints)

Test script:
---------------
<?php

declare(strict_types=1);

function print_str(string $foo) { var_dump($foo); }

$input = [1, 2, 3];
array_map('print_str', $input);

Expected result:
----------------
Type error

Actual result:
--------------
Coercion

Patches

Add a Patch

Pull Requests

Pull requests:

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2017-06-13 11:53 UTC] cmb@php.net
Isn't that to be expected? print_str() is called from array_map()
and the latter is not declared in a declare(strict_types=1) file.
 [2017-06-13 12:36 UTC] spam2 at rhsoft dot net
> Isn't that to be expected? print_str() is called from array_map()
> and the latter is not declared in a declare(strict_types=1) file

shouldn't it be logical that when *all* of your files start with declare(strict_types=1) everything has to use strict typing?
 [2017-07-09 00:33 UTC] gmblar+php at gmail dot com
Same result in:

<?php

declare(strict_types=1);

$input = [1, 2, 3];
array_map(function(string $foo) {
    var_dump($foo);
}, $input);

?>

> Isn't that to be expected? print_str() is called from array_map()
> and the latter is not declared in a declare(strict_types=1) file.

Does that mean that user defined functions are not affected by declare(strict_types=1), if they called by internal functions?


// call_user_func respect strict_types
call_user_func(function(string $foo) {
    var_dump($foo);
}, $input);

// array_filter ignores strict_types
array_filter($input, function(string $foo) {
     var_dump($foo);
});
 
PHP Copyright © 2001-2019 The PHP Group
All rights reserved.
Last updated: Sun Mar 24 19:01:25 2019 UTC