On Mon, 17 Aug 2020 at 05:19, Michael Voříšek - ČVUT FEL <
voris...@fel.cvut.cz> wrote:

> This seems almost as a bug, strict types should apply also for array key
> which is currently not the case.
>
> https://3v4l.org/epv5s



No, it really shouldn't. In hindsight, "strict_types" was a bad name for
that directive; it is not a catch-all "make the type system stricter" flag,
it changes one specific piece of logic: when passing a scalar value to a
function parameter annotated with a different scalar type, should the value
be automatically cast, or require a manual cast. There is no function call
in that code example, so the directive has no effect.


The reason PHP treats $foo[42] as referring to the same element as
$foo['42'] is to support this straight-forwardly useful code:

$options = [
    1 => 'apple',
    2 => 'orange',
    3 => 'banana',
    4 => 'caramel',
];
$selected_option = $options[ $_GET['id'] ] ?? 'vanilla';


Casting between arrays and objects is a whole separate issue, and changed
in PHP 7.2; the RFC explains the issues quite well:
https://wiki.php.net/rfc/convert_numeric_keys_in_object_array_casts


Regards.
-- 
Rowan Tommins
[IMSoP]

Reply via email to