3 ms·
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 appl
by ionfish 18y ago
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 http://metapundit.net/sections/blog/166
- cninja 18y agoI 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);
- apgwoz 18y agoI'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.