Re: Stricter implicit boolean coercions
| From: | Mel Dafert | Date: | Tue, 26 Apr 2022 17:26:53 +0000 |
| Subject: | Re: Stricter implicit boolean coercions | ||
| References: | 1 2 3 4 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-117626@lists.php.net to get a copy of this message | ||
>I see, so as long as there are no bool type hints for function parameters everything would be
>the same.
>
>This would lead to a minor asymmetry for
> $preserve = "yes";
> if ($preserve) # Silently working, true
> array_slice($array, $offset, preserve_keys: $preserve)); # Not working any more
>
>I assume your solution would be to add an explicit cast to bool? i.e. something along the lines
>of
> array_slice($array, $offset, preserve_keys: (bool)$preserve)); # Explicit cast to silence
>implicit conversion
>
>I'm a bit worried about having to keep two different convert-to-bool rule sets in mind
>(implicit vs. explicit) and about the additional casts.
There are already two different convert-to-bool rule sets for
array|object|resource|null, where they will be accepted in
if but rejected when
passed to a function taking a bool param:
```
$x = (object) ['a'=> 1];
if ($x) {
echo "if\n";
}
function takes_bool(bool $param) {}
takes_bool($x); // throws TypeError
```
https://3v4l.org/fCQFL
Adding string|float to that list doesn't seem to be that big of a difference here.