James Ball

How Do You Do, Fellow Kids?

PHP Lambda Functions Approved!

The PHP team recently approved the Short Arrow Functions RFC proposed by Nikita Popov, Levi Morrison, and Bob Weinand.

In the RFC it shows this as an example to give you an idea on how it can be used:

php
1
2
3
4
5
6
$extended = function ($c) use ($callable, $factory) {
    return $callable($factory($c), $c);
};

// with arrow function:
$extended = fn($c) => $callable($factory($c), $c);
"A Laravel example could look like this"
1
2
3
4
5
6
7
// Current
$users->map(function($user) {
    return $user->first_name.' '.$user->last_name;
});

// with arrow function:
$users->map(fn($user) => $user->first_name.' '.$user->last_name);

Short Arrow Functions is targeted for inclusion in PHP v7.4, and you can read more about this on the PHP wik.