3 ms·
My attempt on functional programming in PHP
- adlpz 6y agoThis article is very outdated. PHP has closures, can natively pass callables as arguments and you don't need any of these workarounds: https://www.php.net/manual/en/functions.anonymous.php https://www.php.net/manual/en/functions.anonymous.php It's not idiomatic to do so, but you can totally write sort of functional PHP nowadays without any tricks. Stuff like `array_map` exists since PHP 4.
- lioeters 6y agoIt's true. That whole first section on Functions as Arguments can be achieved as: $inc = function($number) { return $number + 1; }; $x = [1, 2, 3, 4]; array_map($inc, $x);
- bogdanu 6y agoAnd you can also use the new syntax: $inc = fn ($number) => $number + 1; $x = [1, 2, 3, 4]; array_map($inc, $x);