Bug #74327 [Com]: foreach: wrong typecasting breaks strict mode
[email protected] ("spam2 at rhsoft dot net")
| Newsgroups | php.bugs |
|---|---|
| Message-ID | <[email protected]> |
Edit report at https://bugs.php.net/bug.php?id=74327&edit=1 ID: 74327 Comment by: spam2 at rhsoft dot net Reported by: spam2 at rhsoft dot net Summary: foreach: wrong typecasting breaks strict mode Status: Not a bug Type: Bug Package: Arrays related PHP Version: 7.1.3 Block user comment: N Private report: N New Comment: "Strings containing valid integers will be cast to the integer type" - yeah that was written BEFORE strict-types existed but don't make any sense in a file which starts with <?php declare(strict_types=1); and calls functions as in my example - that error happend in the real world while step-by-step change a whole application to strict-typing Previous Comments: ------------------------------------------------------------------------ [2017-03-28 13:43:39] [email protected] Thank you for taking the time to write to us, but this is not a bug. Please double-check the documentation available at http://www.php.net/manual/ and the instructions on how to report a bug at http://bugs.php.net/how-to-report.php http://php.net/manual/en/language.types.array.php > Strings containing valid integers will be cast to the integer type. E.g. the key > "8" will actually be stored under 8. Having [1] and ["1"] be distinct would create chaos. ------------------------------------------------------------------------ [2017-03-28 13:37:52] spam2 at rhsoft dot net Description: ------------ [harry@rh:/downloads]$ php test.php Fatal error: Uncaught TypeError: Argument 1 passed to test() must be of the type string, integer given, called in /mnt/data/downloads/test.php on line 11 and defined in /mnt/data/downloads/test.php:13 Stack trace: #0 /mnt/data/downloads/test.php(11): test(1, 'c') #1 {main} thrown in /mnt/data/downloads/test.php on line 13 ____________________________________ why in the world is the array-key silently cased to an integer - if that would have been the intention the code would be 1=>'c' instead '1'=>'c' besides that this breaks strict mode that conversion are useless cpy-cycles Test script: --------------- [harry@rh:/downloads]$ cat test.php <?php declare(strict_types=1); $arr = [ 'a' => 'a', 'b' => 'b', '1' => 'c', '2' => 'd' ]; foreach($arr as $key=>$value) { test($key, $value); } function test(string $a, string $b) { } ------------------------------------------------------------------------ -- Edit this bug report at https://bugs.php.net/bug.php?id=74327&edit=1