Back to Top

What are Lambdas and Closures in PHP?

lambda-and-closure-in-php

Lambdas and Closures are new features added to PHP with version 5.3 and greater. Both Lambdas and Closures come with some new functionality and the ability to refactor old code to make it clean and intuitive. However, Many developers are unaware of Lambdas and Closures functionalities, so here I come with the article which explains all about Lambdas and Closures in PHP.

In this post, I am going to explain Lambdas and Closures with some real time example code which shows the usage of Lambdas and Closures in PHP.

What is Anonymous functions?

An anonymous function is a function that can not be accessed by its name, but only by the variable references such as references or pointers.

What is a Lambda?

Lambda is used to describe anonymous functions or a Lambda itself is an anonymous function in PHP which can be stored to a variable or passed as an argument to another function.

SYNTAX:

Here is the syntax of a new lambda style in PHP.

As per above syntax, You can call the function without function name and assign it directly to the variable.Lambdas in PHP is an anonymous function which is created without a name. Anonymous functions were introduced in PHP4 which was called using create_function() function. Lambdas are faster than functions created using the create_function function.

Let’s understand a Lambda in PHP with an example:

First of all, let me show you the regular function which you can create normally

This is a very simple function. Above function will perform the summation of two values when calling the function by the function name and passed two argument. Now, Let’s call this function indirectly using a variable which value is the same as the function name:

This code explains you the lambdas in PHP. It is very similar to the above regular function, just function created without function name.lambdas are used as callback functions to other functions and there won’t be giving any name. Though this simple example, will not explain any advantage of anonymous function but this depends on the situation where it is good to write such kind of code.

What are Closures in PHP?

Closures in PHP are anonymous functions which are little more complex than lambdas as it allows interactions with external variables. Variables were defined in the same scope in which the closure was defined and you can use the keyword “use” to inform the external variables which is going to interact will be in parentheses.

SYNTAX:

Here in above syntax, a function is anonymous same as lambdas but change is used “use” keyword and attaches a variable in parentheses to interact the data externally. A closure is a function that wraps the current scope and can have one or more variables attached in order to understand closures you should have a clear idea on scope of a variable in PHP.A closure is a function that has the current scope with one or more variables.

Let’s understand the example of Closures in PHP

First of all, I have created a lambda, to calculate the sum between two parameters by assigning variable $sum to get the output. Now, in this example, I have created an example of Closures in PHP. Here, used $pi as an external variable which used in “use” keyword with function and then within the function to interact or use the value of a variable within it.Variable is passed to or created by the enclosing function can be used inside the closure using the “use” keyword. This shows how you can pass an external variable in use keyword and make closure in PHP and that is the ability to make variables available in the current scope.

Make sure that the change only takes effect after the function declaration if the required variable is passed as a reference.Here is the example using direct variable and variable passed as a reference.

In this example, you can see it’s impossible for a closure to modify the variable in the parent scope. By prepending an & in front of the variable name in the use declaration, the variable is passed as a reference. In that case, changes to the variable inside the closure will affect the outside scope.

Conclusion

Sometimes you need to write a function which will be used only once and Lambda functions are the best option for such throw away functions.Closures help you to manage the code through some external variable.Lambdas and Closures help you to define functions which can be used only once, directly on the spot.

Are you also using Lambdas and Closures in PHP? Do share your review & experience with us.

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Most Popular Posts

How to backup MySQL database in PHP

Posted on 11 years ago

Bhumi

PHP 5 Abstract classes and Methods

Posted on 12 years ago

Bhumi

The Readers’ Poll – December 2012

Posted on 11 years ago

Bhumi