This post originated from an RSS feed registered with Python Buzz
by Phillip Pearson.
Original Post: Hey, PHP *does* have anonymous functions!
Feed Title: Second p0st
Feed URL: http://www.myelin.co.nz/post/rss.xml
Feed Description: Tech notes and web hackery from the guy that brought you bzero, Python Community Server, the Blogging Ecosystem and the Internet Topic Exchange
I haven't been doing much Real Data Processing work in PHP recently, and today I'm getting back into it, writing some comment spam administration screens for PeopleAggregator. I was under the impression that you couldn't create anonymous functions in PHP, and was really missing being able to do stuff like Python list comprehensions.
It turns out, though, that PHP has a create_function function, which will make a function from a couple of strings you give it. This might work fairly well; it seems that there are plenty of limitations, but it might work well for simple things like what I want here. I'm not confident that PHP will clean up the function when you're finished with it, so it might be safer not to use this inside loops, but it's still useful nonetheless.
- - -
Here are some examples of equivalent code in various languages:
I haven't done any Perl for a while, but off the top of my head I think the equivalent there would be:
$ids = map { $_->{comment_id} }, $details;
And I guess Ruby would do something like this:
ids = details.each { |item| item['comment_id'] }
And Javascript + Prototype gets you:
var ids = details.map(function(item) { return item.comment_id; })
- - -
Of course, if you do something like this (which I haven't tested - one assumption in particular that may be wrong is that you can use arrays of strings as array keys), you should be able to use create_function inside loops, as long as you don't change the function text each time:
$function_cache = array();
function mkfunc($args, $code) {
global $function_cache;
$k = array($args, $code);
$f =& $function_cache[$k];
if (isset($f)) return $f;
$f = create_function($args, $code);
return $f;
}