Null Coalesce Handling Revised
| From: | Robert Chapin | Date: | Mon, 23 Nov 2026 16:54:09 +0000 |
| Subject: | Null Coalesce Handling Revised | ||
| Groups: | php.internals | ||
| Request: | Send a blank email to internals+get-133205@lists.php.net to get a copy of this message | ||
I wrote last year about an idea to make it easier to handle variables that are either unset or uninitialized.
https://news-web.php.net/php.internals/126826
That idea fizzled because a Variable Handling Function didn't seem like the best approach, was tricky to name, and had concerns about how to set default values.
I've refined this a bit to contemplate userland functions where we might want or need to coalesce a variable of unknown status. The new idea is to allow a
?? prefix for parameter type hints.
A hypothetical signature...
function array_coalesce(??array $input = null): array;
... could accomplish a few things:
It allows the user's function to define a parameter that is both optional and allows an unset variable to be "passed" without errors.
Type safety can still prevent incorrect variable types.
A default value is supplied, which allows this feature to be used in more situations.
This won't introduce any new names or abbreviations, instead reusing the ?? operator token.
Order of operation problems are solved by calling this hypothetical function instead of using the ?? operator. In other words, if $b is unset and someone attempts if ($a === $b ?? []) we have a missing set of parentheses and a runtime warning. However, `if ($a === array_coalesce($b))` becomes an intuitive and viable approach to testing the value of $b.
Foreseeable questions:
Would these parameters be implicitly nullable?
Is there a significant need for this feature?
-------------
Robert Chapin