3 ms·
Stop Using Pseudo-Types
- boycothn 10mo ago[dead]
- pwdisswordfishy 10mo agoWhy? The article fails to explain. > The introduction of first-class callables simplifies callback handling. You no longer have an excuse to define your callbacks like this: > <?php > $data = array_map(trim(...), [' x', 'z ']); “First-class callables” is that exact syntax.
- donatj 10mo agoExactly. The article kind of circles around the idea that it's bad, but never really lands on why. My experience is with modern annotations, callable and iterable are pretty powerful, convenient, and safe. - https://phpstan.org/writing-php-code/phpdoc-types#callables https://phpstan.org/writing-php-code/phpdoc-types#callables - https://phpstan.org/writing-php-code/phpdoc-types#iterables https://phpstan.org/writing-php-code/phpdoc-types#iterables
- joe_hoyle 10mo agoThe problem is that first class callables actually makes a new object wrapper for each reference, so trim(…) != trim(…). (It can be true in some cases, it depends if the memory is freed for the first reference).
- pwdisswordfishy 10mo agoAnd how is Closure::fromCallable('trim') recommended by the article any better in that respect? $ php8.4 -r "var_dump(Closure::fromCallable('trim') === Closure::fromCallable('trim'));" bool(false)
- joe_hoyle 10mo agoIt is not better, this is a problem with the first-class callable syntax altogether.
- nilslindemann 10mo agoStop using languages where strings are callable :-p
- pwdisswordfishy 10mo agoI think the point of introducing the (...) syntax that for some reason this article apparently denigrates (if I even understand it correctly) is to eventually deprecate that.