Bug #80309 [Nab]: numerical array keys converted to int in strict mode

From: Date: Tue, 03 Nov 2020 09:06:14 +0000
Subject: Bug #80309 [Nab]: numerical array keys converted to int in strict mode
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-230077@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=80309&edit=1

 ID:                 80309
 User updated by:    shop1 at mokraemer dot de
 Reported by:        shop1 at mokraemer dot de
 Summary:            numerical array keys converted to int in strict mode
 Status:             Not a bug
 Type:               Bug
 Package:            Arrays related
 Operating System:   Linux
 PHP Version:        Irrelevant
 Block user comment: N
 Private report:     N

 New Comment:

Thanks for your reply.
Maybe it is a good idea to advance strict_types to
declare(strict_types=2);
and that way no conversions are made and will get us to a more typesafe way.

It is hard to understand/handle putting strings in a "list" and getting a mixed type back.
Using phan also suggests to declare it as e.g. array<string,string>, but if there is any
user/db interaction all those declarations become weak and may fail at runtime.

Maybe this can be asked as a "feature request" to make even arrays more strict.


Previous Comments:
------------------------------------------------------------------------
[2020-11-03 00:39:50] requinix@php.net

You're right: by enabling strict_types you must now take responsibility for making sure that
the types of your function arguments are correct.

Numeric strings as array keys causes multiple problems in other places and is not something that can
be easily changed.
What can be easily changed is adding a "(string)" cast to your code.

------------------------------------------------------------------------
[2020-11-03 00:07:14] shop1 at mokraemer dot de

it is true, this is documented behaviour, but with strict types this becomes a problem. Since array
keys do not keep the original type.
If this is kept, it means every array key must be explicitly converted back to string.
Using e.g. array_keys($array) to pass an array of strings does not work, you always have to reaply
array_map('strval',array_keys($array))

This makes everything errorprone, it is hard to detect those errors, as users/databases,... may be
the source of those values.

------------------------------------------------------------------------
[2020-11-02 22:51:01] requinix@php.net

This is documented behavior for array keys and is not affected by strict_types.

------------------------------------------------------------------------
[2020-11-02 22:49:41] shop1 at mokraemer dot de

Description:
------------
numerical array keys are converted to integers.
In strict mode (typed properties, typed function parameters), this is not desireable.

Test script:
---------------
declare(strict_types=1);
function f(string $s):string{
	return $s;
}

$array=[
	'x'=>5,
	'1'=>6
];

foreach($array as $k => $v){
	echo f($k);
}


Expected result:
----------------
x1

Actual result:
--------------
xPHP Fatal error:  Uncaught TypeError: Argument 1 passed to f() must be of the type string, int
given, called in /tmp/test.php on line 13 and defined in /tmp/test.php:3
Stack trace:
#0 /tmp/test.php(13): f()
#1 {main}
  thrown in /tmp/test.php on line 3



------------------------------------------------------------------------



--
Edit this bug report at https://bugs.php.net/bug.php?id=80309&edit=1


Thread (6 messages)

« previous php.bugs (#230077) next »