PHP Anonymous Functions

Anonymous Functions Speak Volumes in Code

Anonymous functions, or closures, are functions that seem to cause a lot of stress in people’s lives. Let’s simplify this process. An anonymous function is a function without a name.

Let’s look at a regular function. A regular function starts with the function keyword and is followed by the function name; the parameters are enclosed inside of parentheses and the function body is enclosed inside of curly braces.

<?php

function function_name($parameter_name) {
// body
}

?>

The anonymous function drops the function name and assigns the function to a variable.

<?php

$var_name = function ($parameter_name) {
    // body
}

?>
Let’s create an actual example. We’ll show both a standard function and an anonymous function declaration. Let’s start with the regular function. The function is called stuff_dogs_say(). It will just echo out “Dog says Cats Suck” after its been called. We’ve already looked at how to call regular functions in the 
<?php

// Regular Function
function stuff_dogs_say() {
  echo "Dog says Cats Suck<br>";
}

// Calling a regular function
stuff_dogs_say();

?>
Now, let’s convert the regular function into an anonymous function.
<?php

$stuff_dogs_say = function() {
    echo "Dog says Cats Suck<br>";
};

$stuff_dogs_say();

?>

We create a variable called $stuff_dogs_say and assign the anonymous function to it. Since we are assigning something to the variable, we need to end the assignment statement with a semi-colon like we always do. The function is spaced out onto multiple lines for clarity. Since $stuff_dogs_say contains the anonymous function, we can call that function by appending a couple of parentheses to the variable name itself: $stuff_dogs_say().

We can declare parameters and pass arguments to the anonymous function just like we do with regular functions.

<?php

$stuff_dogs_say_two = function( $saying ) {
    echo $saying;
};

$stuff_dogs_say_two( "Cats Stink<br>" );

?>

We declare the anonymous function with the parameter $saying and assign it to the $stuff_dogs_say_two variable. The anonymous function just echoes out the argument that’s passed to it. During the function call on line 7, the argument Cats Stink is passed to the $stuff_dogs_say_two function.

Let’s look at something interesting. We know that we can return any data type from inside of a function. That means that we can return functions as well; I know, mind-blown. We’re going to create a regular function that returns an anonymous function. Let’s jump into an example and walk through it.

<?php

function stuff_cats_say() {
    return function() {
        echo "Dogs Suck";
    };
}

$cats = stuff_cats_say();
$cats();

?>
  1. We start by creating a regular function called stuff_cats_say(). The regular function just returns an anonymous function. The anonymous function contains an echo statement: nothing revolutionary here. We know that we can return anything here so we could have returned an integer (i.e. return 3;) or a string (i.e. return “Dino”;). We’re just returning an entire function this time (i.e. return function() { echo “Dogs Suck”; };)
  2. We assign the regular function stuff_cats_say() to the $cats variable on line 9. Remember how PHP works. It starts on the right-hand-side of the assignment operator. It evaluates the stuff_cats_say() function. That function just returns an anonymous function. That anonymous function is then assigned to the $cats variable. It would be exactly the same as saying: $cats = function() { echo “Dogs Suck”; };
  3. Now that we know that the $cats variable contains the anonymous function, we know how to call that anonymous function: by appending a couple of parentheses to the variable.
  4. The $cats() function call will call the anonymous function. What does the anonymous function do? It just echoes out Dogs Suck.

Why use anonymous functions over regular functions? Sometimes you need to access a variable that’s outside of the scope of the function and for whatever reason the variable can’t be passed as an argument. You can use the “use” keyword with an anonymous function to gain access to the scope of that variable. We’ll look at the “use” keyword in the next article.

 

PHP Variable Functions

ADAPTING AND PERFORMING WITH VARIABLE FUNCTIONS

PHP – P38:VARIABLE FUNCTIONS 

PHP supports the concept of variable functions. What that means is that you can append parentheses to a variable and use it as a function call.

PHP Anonymous Functions

Anonymous Functions Speak Volumes in Code

PHP – P39: anonymous functions

Anonymous functions, or closures, are functions that seem to cause a lot of stress in people’s lives. Let’s simplify this process. An anonymous function is a function without a name.

PHP use Keyword

DIRECTING CODE FLOW THROUGH THE COMPASS OF ‘USE’

PHP – P40: USE KEYWORD

Scope is the variable and method visibility that a certain portion of the program has access to. For example, let’s say that we declare a variable inside of a function (regular or closure). Other functions will not have access to that variable.

Leave a Reply