All Unkept
Posted in: Software development  —  9 April 2005

Accumulator Generator in PHP

Here's my solution to writing an accumulator generator in PHP, listed here because Paul Graham is no longer accepting submissions:

function foo($n) {
  $nname = uniqid(rand(), true);
  $GLOBALS[$nname] = $n;
  return create_function('$i',
    'return $GLOBALS["' . $nname .'"] += $i;');
}

Slightly hacky (*ahem*), but I like the fact you can do tricks like this in PHP when you have to -- in fact something very similar to this comes in useful in my Flatfile package when I need to turn an instance method of an object into a function for use with built-in sorting methods.

Comments §

No comments.

Add comment

Format:

  • Javascript has to be on to get past my spam protection, and cookies, and there is a delay, sorry for any inconvenience!
  • I reserve the right to moderate comments.