PHP 5's anonymous functions

Tags
Tagsphp, programming
Posted
Wed 5 Apr, 2006
Comments
0

I don’t see much use of anonymous functions in php, yet when I write python or ruby, I use constructs like this quite often. You can do it in php though, with create_function. It’s nothing that fancy, it just lets you create a function whenever you feel like it. This can be placed wherever you need a callback in php.

Callbacks in php must be a function name. You can’t refer to methods in the current class, or hack it by making a function that returns an object: function()->probably_a_singleton->hello(). This is where create_function() comes in.

Here’s an example:

$where_sql[] = " $field IN (" . implode(',', array_map(create_function('$str', 'return registry()->db->quoteSmart($str);'), $data['in'])) . ') ';

This is part of a query builder I’m writing for a project. It lets you pass arrays to build queries, and also something like this:

array('in' => array('data' => array(1, 2, 3)))

This maps to the SQL fragment “IN (1, 2, 3)”, and also escapes the operands to make sure the query is relatively secure. I had to call our singleton’d database object through create_function to do this:

create_function('$str', 'return registry()->db->quoteSmart($str);'), $data['in'])

array_map calls this with each field you pass and escapes them.

So all in all, remember your friend create_function. He can save some legwork, and it might help you do things how you would in other languages if you’re coming to php from somewhere else.


Security Code