Req #76412 [Opn->Nab]: Nullable type default value
[email protected] Tue, 5 Jun 2018 09:11:12 GMT
| Newsgroups | php.standards |
|---|---|
| Message-ID | <[email protected]> |
Edit report at https://bugs.php.net/bug.php?id=76412&edit=1 ID: 76412 Updated by: [email protected] Reported by: ionuthrive at yahoo dot com Summary: Nullable type default value -Status: Open +Status: Not a bug Type: Feature/Change Request Package: PHP Language Specification Operating System: All PHP Version: 7.2.6 Block user comment: N Private report: N New Comment: Thank you for taking the time to write to us, but this is not a bug. Please double-check the documentation available at http://www.php.net/manual/ and the instructions on how to report a bug at http://bugs.php.net/how-to-report.php This is by design. Passing null does not always indicate "skip this parameter and use the default", null may also have a semantic meaning. The behaviour you want can be easily achieved in combination with the ?? operator, something like: function get(string $id, ?int $limit = 1, ?string $type = 'standard', ?array $options = []){ var_dump($limit ?? 1); var_dump($type ?? 'standard'); var_dump($options ?? []); } In combination with private class constants, you can also avoid specifying the default value more than once in code, retaining correct information reported by reflection/IDEs etc: class Foo { private const DEFAULT_LIMIT = 1; private const DEFAULT_TYPE = 'standard'; private const DEFAULT_OPTS = []; public function get(string $id, ?int $limit = self::DEFAULT_LIMIT, ?string $type = self::DEFAULT_TYPE, ?array $options = self::DEFAULT_OPTS) { var_dump($limit ?? self::DEFAULT_LIMIT); var_dump($type ?? self::DEFAULT_TYPE); var_dump($options ?? self::DEFAULT_OPTS); } } Previous Comments: ------------------------------------------------------------------------ [2018-06-05 08:55:55] ionuthrive at yahoo dot com Description: ------------ When passing null to a optional nullable type argument, the default value is overwritten. Test script: --------------- <?php function get(string $id, ?int $limit = 1, ?string $type = 'standard', ?array $options = []){ var_dump($limit); var_dump($type); } get(1, null, 'custom'); get(1, 10, null, ['my'=>'option']); Expected result: ---------------- Since 7.1 when the nullable types could be marked as null the situation where one needs to call a function while skipping some parameters is met with overwrite of the default values of the function arguments. For the above test script, when passing null and the argument is specifically declared as a nullable type, the parser should know to take the default value instead of overriding it as NULL. feature-php.php:4: 1 feature-php.php:5: string(6) "custom" feature-php.php:4: int(10) feature-php.php:5: standard Actual result: -------------- feature-php.php:4: NULL feature-php.php:5: string(6) "custom" feature-php.php:4: int(10) feature-php.php:5: NULL ------------------------------------------------------------------------ -- Edit this bug report at https://bugs.php.net/bug.php?id=76412&edit=1