News for hackers. Stuff that matters.
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);

There are no comments on this comment yet.