News for hackers. Stuff that matters.
Re:Higher Order PHP by ionfish on Friday February 06, 2009 @02:11PM (#470183)
It's worth noting that functions created via create_function won't be garbage-collected, which can cause some obvious problems.

This article about partial application may also be of interest to PHP hackers with an interest in functional programming.

http://metapundit.net/sections/blog/166

Re:Higher Order PHP by cninja on Friday February 06, 2009 @03:02PM (#470250)
I personally use a wraper around php's create_function to predefine upto 6 parameters and also return the last statement.

  function lambda( $code )
  {
    if( !preg_match( '/return/', $code ) )
    {
      $code = preg_replace('/^(.*;(?!\s*$))?/s', "$1 return ", $code);
    }

    $code .= ';';
    $response = create_function('$_=null,$_1=null,$_2=null,$_3=null,$_4=null,$_5=null', $code);
	
    if( !$response )
    {
        error_log("lambda($code) failed");
    }
    return $response;
  }

This converts:

  $func = create_function('$v','return htmlenties($v,ENT_QUOTES);');
  array_map($func, $data);
Into:

  array_map(lambda('htmlenties($_, ENT_QUOTES)'), $data);
Re:Higher Order PHP by apgwoz on Friday February 06, 2009 @03:05PM (#470255)
I'm taking care of this by memoizing them, at least when using Fn::lambda() to create a function with create_function. However, the whole return function() {}; should be garbage collected, since internally it's creating an object of type Closure with a magic method __invoke(). It's really quite the hack from what I've seen.