Re: [Concept] Don't cast keys in array to int
| From: | Josh Bruce | Date: | Mon, 17 Aug 2020 17:30:10 +0000 |
| Subject: | Re: [Concept] Don't cast keys in array to int | ||
| References: | 1 2 3 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-111593@lists.php.net to get a copy of this message | ||
Thanks Rowan. I can appreciate that rationale.
Also let’s me in on why we might want to have an indexed array that is not necessarily sequential
- but are integers.
Thanks again.
Cheers,
Josh
> On Aug 17, 2020, at 4:17 AM, Rowan Tommins <rowan.collins@gmail.com> wrote:
>
> On Mon, 17 Aug 2020 at 05:19, Michael Voříšek - ČVUT FEL <
> vorismi3@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]