Using Lambda and Closure functions in PHP

A Closure is essentially the same as a Lambda apart from it can access variables outside the scope that it was created. As you can see above, the Closure is able to access the $user variable. because it was declared in the use clause of the Closure function definition.

In PHP, anonymous functions (also known as closures) can be created using the `function` keyword. Lambda functions are a type of anonymous function that can be assigned to a variable. They are often used for short-lived operations, especially as arguments to higher-order functions like `array_map`, `usort`, or `array_filter`. Here’s a brief overview of lambda functions and closures in PHP:

Lambda Functions:

Lambda functions in PHP are created using the `function` keyword, followed by the parameter list and the function body. They are assigned to a variable and can be passed around like any other variable.

“`php

$add = function ($a, $b) {

    return $a + $b;

};

$result = $add(3, 5);

echo $result;  // Outputs: 8

“`

Closures:

A closure is a function object that has access to variables in its lexical scope, even when the function is called outside that scope. Closures are created using the `use` keyword to import variables from the outer scope.

“`php

$factor = 10;

$multiply = function ($number) use ($factor) {

    return $number * $factor;

};

$result = $multiply(5);

echo $result;  // Outputs: 50

“`

In the example above, the closure `$multiply` has access to the `$factor` variable from the outer scope.

Using Lambda Functions as Arguments:

Lambda functions are often used as arguments to other functions. Here’s an example using `array_map`:

“`php

$numbers = [1, 2, 3, 4];

$square = function ($n) {

    return $n * $n;

};

$squaredNumbers = array_map($square, $numbers);

print_r($squaredNumbers);

// Outputs: Array ( [0] => 1 [1] => 4 [2] => 9 [3] => 16 )

“`

In this example, the `$square` lambda function is used as an argument to `array_map` to square each element in the array.

Lambda functions and closures provide a concise way to define short, reusable pieces of code in PHP. They are especially useful when working with functions that accept other functions as parameters.

Why use lambda function in PHP?

Lambda functions are useful when it is required to use function only once. Basically when we create a function available at the global scope and to use it once within a particular function doesn’t make any sense.

So, a better way is to make that function as a lambda function and make it a part of your custom function/method.

Where can we use Closure in PHP?

Closures are useful while using PHP functions that accept a callback function. PHP has several functions that accept callbacks as a parameter like usort(), array_map(), array_walk(), array_filter(), array_reduce(), etc.

Lambda and Closure functions in PHP

One important thing to remember that, even Core Team has added this functionality in 5.3 but the release of 7.0 anonymous function has been introduced. With anonymous classes, PHP has jumped a new height in the PHP ecosystem.

Conclusion

So the addition of lambda and closure functions in PHP offers some new functionality and the ability to revise old code to be cleaner. It has been adopted in many currently advanced PHP framework. It takes time to understand the importance of it and implement it in the current legacy code but it’s better to be late than never.

Leave a Reply

Your email address will not be published. Required fields are marked *

Proudly powered by WordPress | Theme: Orton Blog by Crimson Themes.