Translate

Partners

In short, lambda functions (or function literals/function constants) are distinguishing from normal procedural functions with their ability to be created within other functions and used as valid syntax. They do not necessarily need to be defined beforehand and called correspondingly. As of version 5.3+ of PHP closures support is added. Let's take a few examples so as to make things clear.

function & (arguments) use (variables) { code }

That's the syntax of a lambda function. Following that structure a basic example would look like so:

CODE :

$Holder = function () { echo "Something"; };

This particular example in which the function is appended to a variable has no actual implementation so let's take a look at another, more detailed example of what function literals represent. For the sake of this, let's create one multi-dimensional array which will store a small piece of information about a group of people with different ages.

What we aim to do now is to sort those that are of legal age (18 or above) from those that are not of adult age yet. Now how are we used to do that with 'normal' functions (meaning without the usage of lambda functions)? Take the following piece of code for instance.

The array_filter() functions accepts two parameters. The first one is the array in which to search and the second argument is how the data should be filtered. Because we need everyone that is not only 18 but above, our second parameter also needs to be a function. This is how we would naturally apply a function within another but let's take an example of how this is being conducted with lambda functions.

Now the above example has the additional drawback that in order for us to or any third-party coder to grasp the idea of our function, he'll need to go upwards the source and find out the purpose of the function. In smaller projects that's okay but considering some 30k LOC repeating this process all the time, this means a total mess to both the author and any third-party inspection. Here is where function literals meddle:

One of the most common usages of implementation of lambda functions are in closures. The very idea of this derives from the fact that in most programming languages when an object is called and used, it's being destroyed for the rest of the execution of the code. With closures we can force that object to be existent after its initial usage as long as there is a reference to it and is not outside the scope of visibility. Now let's exemplify what we just mentioned. For the sake of this, we'll create two anonymous functions, one of which would be placed inside the other which will serve the role of our closure.

CODE :

<?php

$lambda = function($Keeper){

$closure = function($Holder) use ($Keeper){

return $Holder." at ".$Keeper

};

return $closure;

};

?>

We'll take two more variables which will be appended the value of the lambda function with a desired parameter that we choose and visualize the output:

The interpreter recognizes the object as a closure within which there is one static array with a key of 'Keeper' and value of 'is a professional' and a parameter which is also an array but with an empty value for the key '$Holder'. Now let's take another variable which is external for the function and remove the external lambda function that we had before along with its calls and another closure which will be taking a parameter by reference.

Executing the above code, we aim to get the initial value of the variable $Keeper and visualizing it over and over again after each lambda function and its reference. This will result in the following:

CODE :

7
7
10

As we have declared the value of $Keeper as that of '7', it stays the same before it has been parsed through the function. After it has entered the first closure, it still remains '7' because the value of '10' refers only for the objects within the function and after the closure with the reference (&$), we end up with the new value.

Haven't really applied it any developments so far but I guess most of you will after you perceive its functionality and grasp the concept behind it. Thanks for reading