I’m sure this conversation has happened before. But it’s on my mind as I work on some things on wanted to ask internals real quick before getting back to it.
[“2” => “hello”, “4” => “internals”] -> [2 => “hello”, 4 => “internals”]
Meanwhile:
[“hello”, “internals”] -> cast to object -> (“0” => “hello”, “2” => “internals”)
Curious why this it’s the case that specifically requesting a string be a key that happens to be an integer, becomes an int type.
I kind of understand why in the case of object the implicit (PHP-established) integers become strings.
And, of course, casting back puts us back to an indexed array. I’ve picked up that type juggling might be a touchy point - but the focus is on why the key gets cast at all when it’s an array type.
Is it to facilitate the type juggle from an object to an array??
Cheers,
Josh
This seems almost as a bug, strict types should apply also for array key
which is currently not the case.
With kind regards / Mit freundlichen Grüßen / S přátelským pozdravem,
Michael Voříšek
I'm sure this conversation has happened before. But it's on my mind as I work on some things on wanted to ask internals real quick before getting back to it.
["2" => "hello", "4" => "internals"] -> [2 => "hello", 4 => "internals"]
Meanwhile:
["hello", "internals"] -> cast to object -> ("0" => "hello", "2" => "internals")
Curious why this it's the case that specifically requesting a string be a key that happens to be an integer, becomes an int type.
I kind of understand why in the case of object the implicit (PHP-established) integers become strings.
And, of course, casting back puts us back to an indexed array. I've picked up that type juggling might be a touchy point - but the focus is on why the key gets cast at all when it's an array type.
Is it to facilitate the type juggle from an object to an array??
Cheers,
JoshTo unsubscribe, visit: https://www.php.net/unsub.php
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.
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]
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 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.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_castsRegards.
Rowan Tommins
[IMSoP]