Re: coerce() function
| From: | Dmitry Derepko | Date: | Sat, 13 Dec 2025 14:34:32 +0000 |
| Subject: | Re: coerce() function | ||
| References: | 1 2 3 4 | Groups: | php.internals php.internals |
| Request: | Send a blank email to internals+get-129601@lists.php.net to get a copy of this message | ||
> On Dec 12, 2025, at 9:26 PM, Hans Henrik Bergan <divinity76@gmail.com> wrote:
>
> trivial to make in userland.
Sure, almost everything is trivial to make in userland and re-use as a function.
> how about making a composer coerce package first,
> give it some time, check if it becomes popular,
> and if it does catch on, reconsider adding it to core?
I don’t think so. There’s no node-js world with left-pad function.
People prefer to write plain php instead of re-using external function that’s needed to be
downloaded and correctly loaded with autoload. Too much effort to re-use.
Moreover, event if a function is quite popular, it doesn’t mean it should be bundled:
dd / dump / u / any Laravel global functions that’s users
think it’s bundled.
I think it’s better to grep the world with structural search and find some cases:
- condition($a) ? $a : expr()
- if (condition($a)) { $a = expr() }
With more or less popular use-cases:
- $a == ‘desc’ ? ‘desc’ : ‘asc’
- $a == ’true’ ? ’true' : ‘false’
- $a > 10 ? 10 : ($a < 0 ? 0 : $a)
- max(0, min($a, 10)
- in_array($key, $values) ? $key : $default
- array_key_exist($key, $values) ? $values[$key] : $default
- isset($values[$key]) ? $values[$key] : $default
- any similar
It could be done if I prepare RFC and it will be necessary. Firstly, I’d like to discuss the idea
itself.
----------
Best regards,
Dmitrii Derepko.
@xepozz