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: 2021-05-28 11:02 UTC
Votes:3
Avg. Score:4.0 ± 0.8
Reproduced:3 of 3 (100.0%)
Same Version:1 (33.3%)
Same OS:2 (66.7%)
From: fredemmott@php.net Assigned: cmb (profile)
Status: Not a bug Package: Scripting Engine problem
PHP Version: 7.1.6 OS: Debian Jessie
Private report: No CVE-ID: None
 [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);
});
 [2021-05-28 11:02 UTC] cmb@php.net
-Status: Open +Status: Not a bug -Assigned To: +Assigned To: cmb
 [2021-05-28 11:02 UTC] cmb@php.net
This is not particularly related to internal functions; rather
whether strict_types apply depends on where a function is
*called*[1].  Internal functions are practically defined in non
strict_type files.

If anybody feels this behavior should be changed, please pursue
the RFC process[2].

[1] <https://www.php.net/manual/en/language.types.declarations.php#language.types.declarations.strict>
[2] <https://wiki.php.net/rfc/howto>
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Mar 28 11:01:27 2024 UTC