Bug #74327 [Com]: foreach: wrong typecasting breaks strict mode

From: Date: Tue, 28 Mar 2017 14:09:00 +0000
Subject: Bug #74327 [Com]: foreach: wrong typecasting breaks strict mode
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-208126@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=74327&edit=1 ID: 74327 Comment by: kelunik@php.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: All numeric array keys are automatically casted to integers, unfortunately. Previous Comments: ------------------------------------------------------------------------ [2017-03-28 14:08:51] bwoebi@php.net That's how arrays always worked, one just never really noticed due to us not having a strict mode … [I'm not going to discuss here whether the strict mode was a bad idea …] Just don't use strict mode then. ------------------------------------------------------------------------ [2017-03-28 13:48:43] spam2 at rhsoft dot net any by all respect it's idiotic that you cast something which is a string by intention to a integer and force me to do another explicit cast back to string foreach($password_references as $version=>$hash) { $generated = $cl_api->user_db->generate_password_hash('autotest_pwd', (string)$version); ------------------------------------------------------------------------ [2017-03-28 13:47:15] spam2 at rhsoft dot net "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 ------------------------------------------------------------------------ [2017-03-28 13:43:39] requinix@php.net 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

« previous php.bugs (#208126) next »