PHP – Anonymous Functions and Closures (Part I)

Anonymous functions are functions that are defined without being bound to a proper name. Typically, anonymous functions are used only a limited number of times and for a specific purpose; you could think of them as “throw-away” functions if you’d like.

Let’s consider the following example which illustrates a standard function used as a callback:

function percentVowels_callback($word) {
$word = strtolower($word);
$chars = count_chars($word);
$numVowels = 0;
foreach (array("a", "e", "i", "o", "u") as $vowel) {
$numVowels += $chars[ord($vowel)];
}
return $numVowels / strlen($word);
}

$animals = array("dog", "cat", "birs", "ape",S"urchin");
$percentVowels = array_map("percentVowels_callback", $animals);

The array_map() function accepts the name of a function and an array, and produces a new array resulting from applying the callback function to each element of the input array.

The callback function I’ve defined accepts a string and returns the percentage of which is made up of vowels.

Since the function is only used once by this array_map() statement, it may make sense to refactor percentVowels_callback() as an anonymous function. Typically the purpose of a function is to eliminate repetitive code and build reusable components… but the purpose of anonymous functions is different. Anonymous functions group together a related set of statements.

Earlier versions of PHP (starting at version 4.0.1) provided limited support for defining anonymous functions with create_function(). Here’s an example which shows the call to array_map() with percentVowels_callback() refactored as an anonymous function using this create_function().

$percentVowels_callback = create_function('$word', '
$word = strtolower($word);
$chars = count_chars($word);
$numVowels = 0;
foreach (array("a", "e", "i", "o", "u") as $vowel) {
$numVowels += $chars[ord($vowel)];
}
return $numVowels / strlen($word);');

$percentVowels = array_map($percentVowels_callback, $animals);

Depending on your preference and coding style, the callback can be defined inline, as well.

Depending on how the application is organized, you may need to jump to a different section of the code file or to a different file all together to inspect the contents of the function when you are tracing through code, and then find your way back afterwards to the calling location. The function’s body can be available visually where it is most pertinent.

$percentVowels = array_map(create_function('$word', '
$word = strtolower($word);
$chars = count_chars($word);
$numVowels = 0;
foreach (array("a", "e", "i", "o", "u") as $vowel) {
$numVowels += $chars[ord($vowel)];
}
return $numVowels / strlen($word);'), $animals);

The create_function() function accepts two strings–the first listing the variable names to serve as the anonymous function’s arguments, the second containing the code for the function’s body–and returns a unique string which can be used to identify the function.

create_function() does have a few drawbacks, though. Because the argument list and function body are provided as strings, you must be careful to make sure certain characters within the string are escaped properly. You need to escape any single-quotation marks that appear if your strings are single-quoted, or you need to escape double-quotation marks and dollar signs if your strings are double-quoted. Moreover, you also lose the benefits of any syntax highlighting your IDE may provide since it highlights the strings as… well, strings! Overall, the approach is cumbersome and clunky.

As of version 5.3, PHP will offer better support for anonymous functions and a new syntax which supports closures. The new syntax for anonymous functions is more similar to the manner in which JavaScript and other event-driven languages define them.

$percentVowels_callback = function($word) {
$word = strtolower($word);
$chars = count_chars($word);
$numVowels = 0;
foreach (array("a", "e", "i", "o", "u") as $vowel) {
$numVowels += $chars[ord($vowel)];
}
return $numVowels / strlen($word);
};

$percentVowels = array_map($percentVowels_callback, $animals);

Gone is the clumsiness of create_function() and its string arguments. The anonymous function’s arguments and body are provided as PHP code which can be highlighted correctly by your IDE.
The one caveat to the new syntax is that there MUST be a trailing semi-colon after the function’s closing brace, as $percentVowels_callback = some value is a regular assignment statement.

One is also able to define the anonymous function using the new syntax inline as well.

$percentVowels = array_map(function ($word) {
$word = strtolower($word);
$chars = count_chars($word);
$numVowels = 0;
foreach (array("a", "e", "i", "o", "u") as $vowel) {
$numVowels += $chars[ord($vowel)];
}
return $numVowels / strlen($word);}, $animals);

Don’t miss the second part.

Leave a Reply

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

*