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.
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).
None
PHP 7.3
N/A
2/3 majority required
N/A
Discussion: http://google.com