note 121980 added to function.array-walk
| From: | 01001coder at gmail dot com | Date: | Tue, 05 Dec 2017 22:19:52 +0000 |
| Subject: | note 121980 added to function.array-walk | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-209315@lists.php.net to get a copy of this message | ||
I noticed that :
PHP ignored arguments type when using array_walk() even if there was
declare(strict_types=1) .
See this code as an example ...
<?php
declare(strict_types=1);
$fruits = array("butter" => 5.3, "meat" => 7, "banana" => 3);
function test_print(int $item2, $key) {
echo "$key: $item2<br />\n";
}
array_walk($fruits, 'test_print');
?>
The output is :
butter: 5
meat: 7
banana: 3
whilst the expecting output is :
Fatal error: Uncaught TypeError: Argument 1 passed to test_print() must be of the type integer
because "butter" => 5.3 is float
I asked someone about it and they said "this was caused by the fact that callbacks called from
internal code will always use weak type". But I tried to do some tests and this behavior is not
an issue when using call_user_func().
----
Server IP: 208.43.231.9
Probable Submitter: 51.223.91.96
----
Manual Page -- http://php.net/manual/en/function.array-walk.php
Edit -- https://master.php.net/note/edit/121980
Del: integrated -- https://master.php.net/note/delete/121980/integrated
Del: useless -- https://master.php.net/note/delete/121980/useless
Del: bad code -- https://master.php.net/note/delete/121980/bad+code
Del: spam -- https://master.php.net/note/delete/121980/spam
Del: non-english -- https://master.php.net/note/delete/121980/non-english
Del: in docs -- https://master.php.net/note/delete/121980/in+docs
Del: other reasons-- https://master.php.net/note/delete/121980
Reject -- https://master.php.net/note/reject/121980
Search -- https://master.php.net/manage/user-notes.php