Anonymous functions

PHP 5.3 introduced the ability to create and use functions on the fly without naming them. When this technique is employed we refer to those functions as anonymous. Probably the most common situation for anonymous functions are when you need to specify a callback function. Here’s an example:

{show example with usort}

This example takes an array and sorts it alphabetically by last letter, rather than first. The usort function is one of many built into PHP that accepts a callback function.

You don’t have to use the PHP built-in functions to use anonymous functions though. You can create your own functions and methods that accept functions as parameters. Here’s one example:

{show example}

However, you can get the exact same behavior in both cases by declaring the function normally and passing a string representing the name of the function. One reason why you might prefer anonymous functions is that you don’t have to worry about naming collisions. Two functions can’t have the same name in PHP or a fatal error gets thrown.

If you want to access your anonymous function multiple times, you can assign it to a variable, like this:

{show example}

Technically, the variable $myFunc is an object of the PHP internal class Closure. The Closure class cannot have properties and has no callable methods. So, from a practical standpoint, using it is little different from having a string assigned to your variable that represents a function’s name. The only difference is that you aren’t at risk of having a naming collision with another function, and if you call ‘is_object’ on it, it’ll return true.

If you want to do recursion with an anonymous function, you’ll need to accept an argument that represents your function. When invoking it, just pass the variable representing your function as that argument. Here’s the classic factorial function as an anonymous function:

{show example}

You can use type-hinting to ensure that you’ve been passed a function. Just type hint as ‘Closure’. But there isn’t a way to be sure that you’ve been passed the same function, so recursion with anonymous functions definitely has its drawbacks.

PHP provides a handy method to use to test whether a variable can be invoked like a function before you make the attempt. It’s called ‘is_callable’. Technically, it accepts 3 arguments, but simply passing in the variable you want to test will get you a true/false response as to whether or not you can use it. Here it is in some code:

{show example}

Anonymous functions allow you to be very flexible with your coding. If you’re a fan of design patterns, anonymous functions are a handy way to implement the Command pattern. That’s one where you set up an implementing object of some sort that takes and invokes commands, without needing to know anything about them. As the name suggests, it’s a good pattern to use when you need the ability for your objects to do lots of different things that can’t all be defined in advance.

January 31 2010 10:59 pm | Schmategories

Comments are closed.