Functional approach in PHP

The code snippet below will only work in PHP-5.3+!.
Isn’t it the famous functional programming with functions as first class citizens, closures and other functional bla-bla?

function one($a = 0) {
    return function(callable $b) use ($a) {
        return call_user_func($b, $a);
print_r(call_user_func(one(1), function ($x) { return $x + 4; } ));

This was created just for fun. As you may see, in place of callable you can put any valid PHP callable, a form ['Class', 'method']

Use your imagination for other real things, but also try to stay not cursed later by those who will inherit your untraversable codebase :LOL:

Добавить комментарий

Ваш e-mail не будет опубликован. Обязательные поля помечены *

Этот сайт использует Akismet для борьбы со спамом. Узнайте как обрабатываются ваши данные комментариев.