Saturday, March 7, 2009

Anonymous Functions and Closures

Of all the new goodies that are promised in PHP 5.3, the two which I think I am the most excited about are anonymous functions and closures.

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("aardvark", "elephant", "iguana", "orangutan",
"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. For the sake of simplicity, I am only considering the letters A, E, I, O and U to be vowels, even though sometimes Y and rarely even W can be treated as vowels as well.

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);
So far I've shown you a rather uninspired example using an anonymous function, so let's expand it a bit and make things more interesting. Suppose the application should reduce the list of percentages to those that are equal to or greater than a user-specified value. For this we can use array_filter().

The array_filter() function accepts an input array and the name of a function, and produces a new array resulting from applying the callback function to each element of the input array. If the callback returns true then the element tested is included in the final output array. We have a slight problem, though-- the documentation for array_filter() shows it only passes one value to the callback (the current array element to be examined). We need access to user-specified value as well. Closures will allow us to "reach out" outside the scope of the percentFilter_callback() function to see the value of $userPercent.
$userPercent = 0.5;
$userList = array_filter($percentVowels,
function($percent) use ($userPercent) {
return ($percent >= $userPercent);
});
Closures allow you controlled access to values to the parent scope of a function. The new syntax introduces the use keyword to specify which variables should be imported. The anonymous function is passed the current element of the $percentVowels array by array_filter(), but it has access to the value of $userPercent as well which is needed for the comparison.

Closures in PHP import the variable from the parent scope the same as if it were passed as an argument to the function and manipulating the variable does not have an effect outside the function (unless it was passed by reference).

However, it is important to keep in mind that closure is not the same thing as using global. global reference variables within the global scope of the script. Closures on the other hand can only bind to variables in the parent scope of a function, which make using them considerably safer than using global. I'll leave you with this final example which highlights this difference between them:
$x = 42;

function foo() {
function fizz() {
global $x;
echo $x;
}
bar();
}

function bar() {
$bizz = function () use ($x) { echo $x; };
$bizz();
}
For more information on anonymous functions and closures, check out these pages:

4 comments:

  1. I didn't know this was coming! Sweet Jesus! Thanks for the posting, doctor sir!!

    ReplyDelete
  2. These are the features I was waiting for!
    I'm happy to have them implemented, but on the other side I'm feeling like PHP evolves without a clear guideline; these functionalities look like patches for something that was missing.
    Anyway, thanks for sharing!

    ReplyDelete
  3. Great article and insightful! Thanks

    ReplyDelete
  4. Awesome, thanks - i didnt know closure and anonymous functions were on their way.

    Sean

    ReplyDelete