PHP RFC: Coercing array keys in strict mode deprecation


This RFC proposes to emit a strict-standards deprecation notice when PHP would coerce array keys' types (e.g. float to int), in case the page uses strict_types = 1.

For example:

<?php declare(strict_types = 1);
$array = [];
$array[42.42] = 123;
// 7.3+ Deprecated / Strict Standards: invalid key float(42.42), assuming int(42)
// 8.0+ TypeError invalid array key type "float", expecting int or string

Except the emitted notice, code will continue to behave as it does currently, until the notice is removed and replaced with a TypeError in PHP 8.0. If the RFC is accepted, the removal version is binding, but an RFC to block the removal could be used if considered necessary.

Impact on generics

This proposal is friends with generics, as this change won't directly influence future decisions about them.

For example, arrays could be declared as Array<KT, CT> where KT is mixed and CT is mixed thus allowing any key type to be used.

However in non-strict, people will continue to rely on coercion of keys, so when type arguments are not specified, the array “constructors” could continue to default to <int|string, mixed>.

$array = [22.22 => 123, "foo" => "bar"];
$array = array(22.22 => 123, "foo" => "bar");
// will be equivalent (at least in non-strict mode) to:
$array = <int|string, mixed>[22.22 => 123, "foo" => "bar"];
$array = array<int|string, mixed>(22.22 => 123, "foo" => "bar");

If non-strict users desire it, in future this can be changed so that type arguments would default to <mixed, mixed> instead. This would have the effect of eliminating the coercion also in non-strict mode, unless code is changed to explicitly define the type arguments (i.e. from [] to <int|string, mixed>, which would bring the coercion back).

Backward Incompatible Changes


Proposed PHP Version(s)

PHP 7.3

Open Issues



2/3 majority required

Patches and Tests



Discussion: http://google.com

rfc/non_coercing_array_keys_in_strict_mode.txt · Last modified: 2017/09/22 13:28 (external edit)