Anonymous functions can be used to contain functionality that need not be named and possibly for short-term use.
A sorting and filtering example
While true anonymous functions won’t be available until PHP 5.3, you can use create_function to generate functions on the fly by passing them a signature and string containing valid php code. These functions are perfect for use with array sorting functions like usort or uasort as well as array_filter.
For this example, suppose we have the following data:
- $sam = new StdClass;
- $sam->minutes_played = 1300;
- $sam->goals = 5;
- $sam->fouls = 10;
- $max = new StdClass;
- $max->minutes_played = 1900;
- $max->goals = 6;
- $max->fouls = 10;
- $joe = new StdClass;
- $joe->minutes_played = 1100;
- $joe->goals = 4;
- $joe->fouls = 8;
- $team = array($sam, $max, $joe);
You’ve been tasked with reporting on the data, specifically, finding who has the most minutes, goals, and fouls. Traditionally, you’d be inclined to write three functions that sort the array by field you are interested, and return the elements with the highest values. It’s a bit of a brute force approach, but let’s see how we can do this with a single function. Below is that function.
- * Returns an array of all elements in the array that have the highest value in a given field.
- * @param string field
- * @param array stats data
- * @return array
- public function findMaxStat($field, $data)
- // sorting comparision
- $sort_code = '
- if ($a->' . $field . ' == $b->' . $field . ') return 0;
- return ($a->' . $field . ' < $b->' . $field . ' ? -1 : 1);
- // get the data sorted highest to lowest by the field we desire.
- $data = array_reverse($data);
- // remove all elements of the array that do not match the max value
- $max = $data->$field;
- $filter_code = 'return ($a->' . $field . ' == ' . $max . ');';
- return $values;
What’s going on here?
- First, to use the function we simply pass it the field for which we want to get the maximum value and our data array.
- Line 11, defines the body of a function that will be used to sort the array by that field. You’ll have to follow closely the use of quotes to build this string. If we’re looking for most minutes, then the code for this function would look like ‘if $a->minutes == $b->minutes) return 0’ and so on, where $field is replaced by the string ‘minutes’;
- Lines 17 and 18 do the hard work, usort uses our anonymous function to sort the data and then reverse it so that it’s ordered from highest to lowest. You could also achieve this by changing what th $sort_code function returns and eliminating the call to array_reverse altogether.
- In line 21, We get our maximum value for the field we care about from the first element in our sorted array.
- Lines 22-23 are here to ensure that we return all the elements that have the max value of our field. This acocunts for situations where more than one element may be "tied for first place", so to speak.
- Finally, we return the values that we’ve found.
What’s the big deal here? Well, with one function we quickly get the rows we care about.
- // returns $sam and $joe
- $max_fouls = findMaxStat('fouls', $team);
- // returns $max
- $max_minutes = findMaxStat('minutes_played', $team);
- // returns $max
- $max_goals = findMaxStat('goals', $team);
More importantly, if we add more fields to our data, we don’t have to write more code!
Used correctly, anonymous functions provide very customized functionality that can save you from having to write more code. Also, by learning to use PHP’s built-in array manipulation functions, you’ll quickly learn how to slice and dice nested arrays and arrays of objects without resorting to complicated loops and if statements. I’m not sure what the performance implications might be of using the technique, but if it saves you from writing long, complicated code-blocks it’s worth the trade off. There’s a lot of existing discussion about create_function, and its impact on memory usage and DANGER!