Sharing technology, ideas, insights!
Call: +91 710 466 0336         Email:


Lambda functions and Closures in PHP 5.3

PHP 5.3 has introduced a whole new range of cool features like Namespaces, late static binding, new error levels, to name a few. But the functionality that I found most handy for day to day programming is the Lambda functions and Closures that found their way in PHP through version 5.3

Lambda functions
Lambda functions are simple use-N-throw functions that can be defined anywhere and anytime. They can greatly enhance your code that makes use of PHP functions with callback functionality. Before Lambda functions were made available in PHP, one had to either define a regular PHP function explicitly somewhere in the code or create a function using create_function(). But both these comes at the cost of code maintenance.

Well … enough talking. Let us see some code to understand what Lamba functions are and how to use them. All my example code will be based on following array which is a simple two dimensional array of names and scores of users.

Now to get a list of top scorers (with score of 8 and above) I can use array_filter() which accepts a callback function as a second argument to process every array element passed to it. And instead of writing a function somewhere else, I will write the function in place.

The above example will correctly return the names and score for Mark, John and Bob. But the most important thing here is, I could very easily define an inline function without bothering to name it. I know anonymous functions are possible by using create_function() but the requirement of writing the whole function code in quotes is just too annoying after writing few lines of code.
So as you see, no special efforts are required to create lambda functions since PHP 5.3


Closures are similar to Lamda functions but they go a step further by adding the capability of interacting with variables defined outside the scope of the closure function. You can bind the variables with the function in a special way so that they can used inside a closure without requiring to declare them as globals. Let us see what I mean with the following example

The above code will return the list of users with score as mentioned in $score variable (5 in this case). Let us now understand the code itself. The code we are interested in starts at line 7. Here we define a closure function to return an array element if its score matches the required score. Notice the function signature here

The function is going to be used as a callback function for array_filter and thus it must accept exactly one parameter, the array element passed to it. But we also need $score in order to compare the scores. This variable is available in the same scope where this closure is defined. And thus we can make this variable available to the closure with a special use clause in closure signature. And now $score is available for use in your closure. Pass by reference is also permissible with the use clause making it possible to modify it from within the closure.
Also, notice that the closure function is assigned to a variable just like we do in create_function but with the simplicity of writing a regular function. No escaping quotes, no concatenating the strings.

Closures and OOP

Closures are not limited to procedural programming only. You can use it in classes in exactly the same way. Following are some of the important points to remember when using closures with classes and objects –

  • $this cannot be used inside a closure or cannot be made available with use clause.
  • The private members of a class won’t be available inside a closure. This is for a simple reason that closures are evaluated in their own scope or environment.
  • Keyword static cannot be used when defining a closure.

A workaround to use $this in closure is to assign $this to some other variable and then pass it through use clause. For ex.

Now that you know the basics of Lamba and closure functions, take some time out and explore some more, write code and most importantly share your findings with us through comments.

About the Author


  1. Pingback: Tweets that mention Lambda functions and Closures in PHP 5.3 at SANIsoft – PHP for E Biz --

  2. Pingback:

  3. Pingback: SaniSoft Blog: Lambda functions and Closures in PHP 5.3c | Development Blog With Code Updates :

  4. Pingback: SaniSoft Blog: Lambda functions and Closures in PHP 5.3c | PHP

  5. Great . Lambda or Anonymous functions . From the name itself is clear anonymous 🙂 . A function assigned to a variable .

    Thanks once again for your hard work on writing a wonderful post.

  6. Pingback: This Week’s Link List (October 15, 2010) - Brian Swan - Site Home - MSDN Blogs

  7. Pingback: This Week’s Link List (October 15, 2010) : Brian Swan's Blog : The Official Microsoft IIS Site

Leave a Reply