Lambdas And Closures

PHP

When building our applications in Laravel, we often make use of the routing system. The most basic example of this is by using the Route facade, and then appending a HTTP verb, the URI and some weird function looking thing:

<?php

Route::get('my/url', function()
 {
    return 'This is my url!';
 });

This is not something new, or something that we are not used to seeing. But what exactly is going on? Some intermediate to veteran developers will recognize the anonymous function, or lambda function (a function without a name) that we are passing to the get method. However, this may seem confusing to new developers.

What Are They?

As stated earlier lambda's are anonymous functions. That is, a function without a name. PHP allows you to create these lambda functions and assign them to a variable, or even pass them as arguments to a function (this is what we are doing with Laravel's routing).

<?php

// Create an anonymous function and assign
// it to a variable.
$myFunction = function() {
    return 'This is a lambda function';
};

// Calling our anonymous function.
echo $myFunction();

In the above code sample, we are creating an anonymous function and assigning it to a variable named $myFunction. After this, call our function. This looks similar to what we are used to, but we add the $ sign in front since we referencing our variable.

The above code example would print output this to the browser:

This is a lambda function

We could even define function parameters, just like with normal functions:

<?php

// A simple 'hello' function
$hello = function($name) {
    return 'Hello '.$name;
};

echo $hello('Jill');

The code above would output something like this (depending on the name passed in):

Hello Jill

But Why?

These anonymous functions are useful when we as developers need to do something right then and there, but do not necessarily want, or need to create a dedicated function for the task. Like when we define routes in Laravel, it would be cumbersome to have to create a new function in the global scope every time we wanted to define a new route.

Another use of anonymous functions is to pass them as function arguments. For example, let's say we are a developer writing a function that will filter an array. We know that we need to operate on an array, and we need to perform some sort of filtering algorithm, or function on each element of the array. So we start writing our filtering function, and we get pretty far. Then a few days later we need to filter another array in a different way. So we think "All right, I'll just write a new filtering function". This continues for a while, and your code contains functions that look like this:

<?php

// Filtering madness.
filterOnlyNewRegisters($array);
filterHavingMoreThanThreeOrders($array);
filterContainsLetterS($array);
filterContainsLetterT($array);
filterContainsLetterI($array);
filterContainsNumber3($array);
// ... and so on

That is no fun. In fact, that is a nightmare, especially if you are only using the function once! Lambdas to the rescue!

Lambdas as Function Arguments

Thinking back on our previous hypothetical scenario, we can be thankful that there is a built in PHP function named array_filter. This function filters elements of an array using a callback function. That callback function will be the lambda function that you pass in as an argument.

However, we can create our own function that utilizes anonymous functions as an acceptable argument. The first thing we have to do is define what our function is going to do.

Our function will be a simple utility function called if_null_then. It will check if a value is null, if it is, then it returns a default value $then, or does something the developer wants. If the value is not null, it is simply returned as the value of the function. Make sense? So we could use the function we are going to build like this:

<?php

function iHateNulls()
{
    $checkValue = null;

    // The value of $checkValue would be `I hate nulls` after this line:
    $checkValue = is_null_then($checkValue, 'I hate nulls');

    $checkValue = 'candy';

    // The value of $checkValue would be 'candy' after this line:
    $checkValue = is_null_then($checkValue, 'I hate nulls');

}

But what about actually doing something when the value is null, instead of just returning a value? What would that look like?

<?php

function iStillHateNulls()
{
    $checkValue = null;

    $checkValue = is_null_then($checkValue, function()
    {
        // The value was null, so let's just exit the script.
        exit;
    });
}

Okay, that's pretty cool. But what would it look like to actually implement this function? Well, here is the completed code for this function (taken from my in-progress open-source library):

<?php

if (!function_exists('if_null_then'))
{
    /**
    * Returns a value based on whether another value is null.
    *
    * If you need to check if a value exists in an array, such as a checkbox
    * input, consider using the `array_key_exists_then_or` for a simple inline
    * approach.
    * 
    * @param  mixed          $check The value to check for null.
    * @param  \Closure|mixed $then  The value to return if $check is null.
    * @return mixed
    */
    function if_null_then($check, $then)
    {
        if ($check === null)
        {
            if (is_object($then) and $then instanceof Closure) { return $then(); }

            return $then;
        }

        return $check;
    }
}

The important bits for this post are the lines of code that live inside the is_null_then() body. First we are just checking if $check is actually null. If it is not, we return $check. Simple enough.

When $check is null however, we first check to see if the value of $then is an instance of the Closure class. If it is we return from the function by calling the closure, which is done by treating the variable as a function. Notice the { return $then(); } at then of line 20? Lastly, if the value of $then is not an instance of a Closure, we simply return it.

That's it! We now know how to make a simple function that can either accept a regular-old-vanilla value or a really cool inline function. Hope everyone else has had fun. If you found this useful in any way, leave a comment!

Start the Discussion

Leave a comment

Subscribe to our newsletter