The Artima Developer Community
Sponsored Link

Python Buzz Forum
Hey, PHP *does* have anonymous functions!

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Phillip Pearson

Posts: 1083
Nickname: myelin
Registered: Aug, 2003

Phillip Pearson is a Python hacker from New Zealand
Hey, PHP *does* have anonymous functions! Posted: Sep 7, 2006 7:50 PM
Reply to this message Reply

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
Latest Python Buzz Posts
Latest Python Buzz Posts by Phillip Pearson
Latest Posts From Second p0st

Advertisement

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.

My original thought was to use array_map with a named function, but by the time you've declared a function (wrapped appropriately so PHP doesn't complain when you call the parent function again) the amount of code you have to write to do it that way is probably equivalent to just using a foreach loop...

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:

Python:

ids = [item['comment_id'] for item in details]

PHP with foreach:

$ids = array();
foreach ($details as $item) {
   $ids[] = $item['comment_id'];
}

PHP with local function and array_map:

if (!function_exists("__temp_ajksdf")) function __temp_ajksdf($item) {
   return $item['comment_id'];
}
$ids = array_map("__temp_ajksdf", $details);

PHP with anonymous function and array_map:

$ids = array_map(create_function('$item', 'return $item["comment_id"];'), $details);

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;
}

Comment

Read: Hey, PHP *does* have anonymous functions!

Topic: Experience is the name ... Previous Topic   Next Topic Topic: Thiruvonam

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use