PHP RFC: Coercing array keys in strict mode deprecation
- Version: 0.1
- Date: 2017-07-27
- Authors: Wes (Twitter@WesNetmo)
- Status: Draft
- First Published at: https://wiki.php.net/rfc/non_coercing_array_keys_in_strict_mode
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.
<?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
$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
<int|string, mixed>, which would bring the coercion back).
Backward Incompatible Changes
Proposed PHP Version(s)
2/3 majority required
Patches and Tests