> Le 8 avr. 2019 à 12:24, Stephen Reay <php-li...@koalephant.com> a écrit :
>
> if your target ’type’ is `?int` and you pass an empty string, wouldn’t you
> expect to get `null` rather than `0` ?
You should get the same value than you get in:
```php
declare(strict_types=0);
function foo(?int $x) {
return $x;
}
foo("");
```
BTW, this is an argument to the have nullable casting functionality in core,
rather than leave each developer to craft their own solution. Because PHP does
already such nullable castings in several occasions (typed parameters, typed
return values, and soon typed class properties, when you are in strict_types=0
mode), we should offer the programmers a built-in evident way to do the same
type of casting, with the same semantics in occasion they want to do it
manually. Otherwise, one developer will decide to map the empty string to null,
the other to 0 and the third to NAN, and they get problems when they refactor
the code in a way that a manual casting is replaced by an automatic implicit
casting or vice versa, and the semantics differs on some cases.
—Claude
--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php