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

[email protected]
Newsgroups php.bugs
Message-ID <[email protected]>
Edit report at https://bugs.php.net/bug.php?id=74327&edit=1

 ID:                 74327
 Updated by:         [email protected]
 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:

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.


Previous Comments:
------------------------------------------------------------------------
[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] [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
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.