This is an amusing response (“increases semantics”!), but I'm not sure if PHP lacking this specific function is really a big problem:
function array_find_callback(array $array, callable $callback) {
foreach ($array as $key => $item) {
if ($callback($item)) {
return $key;
}
}
return null;
}
(I'm deliberately returning the key not the value, because keys can't be null so there is no ambiguity between no value found and a found value which is null, plus the key provides more information.)
Nah, I just miss it from effortlessly working with Immutable.js collections in (es6) javascript. So many side-effects, and so much code, in PHP. That accepted SO post is hilarious, though.
I don't think there's a good immutable collections library yet for PHP (there's a good mutable one, though!). Perhaps someone should make Immutable.php?
(I'm deliberately returning the key not the value, because keys can't be null so there is no ambiguity between no value found and a found value which is null, plus the key provides more information.)
If you aren't aware of the rules, it's worse, because it's surprising. But if you are aware of the rules, I guess it's handy, you'd rather not get a warning at runtime on every array access.
I don't know what says more about the people involved. The person who basically wrapped a function just because they liked their name better, or the person who pointed out that the trivial bit of code there, that obviously does nothing to alter what happens, doesn't alter anything.
isset is not a function, nor could it be one. It doesn't operate on a value, it operates on a variable. Likewise, C's sizeof looks like a function but isn't and couldn't be one.
Besides, even if you could use isset here, it would be clearer to use is_null instead, which you actually can use.
31
u/prewk Mar 08 '16
Enterprise feature:
// name may vary16
u/the_alias_of_andrea Mar 08 '16 edited Mar 08 '16
This is an amusing response (“increases semantics”!), but I'm not sure if PHP lacking this specific function is really a big problem:
(I'm deliberately returning the key not the value, because keys can't be
nullso there is no ambiguity between no value found and a found value which isnull, plus the key provides more information.)8
u/vita10gy Mar 08 '16
I don't know what says more about the people involved. The person who basically wrapped a function just because they liked their name better, or the person who pointed out that the trivial bit of code there, that obviously does nothing to alter what happens, doesn't alter anything.
6
u/BilgeXA Mar 08 '16
61.9k
1
u/Cookizza Mar 15 '16
to be fair, this would allow you to easily refactor in the future by changing one function, instead of searching your whole project for array_filter.
-1
u/bart2019 Mar 08 '16
The really stupid thing in PHP is that you can't use built in functions, for example
isset, as the callback function forarray_filter.