Okay, let’s say you managed to ensure that your program doesn’t exhaust the heap with anonymous functions, and that you understand the edge cases involved in function-type variables. There are still lots of ways for create_function to bite you in the glutes.
The program argument to create_function is a string. This has two immediate knock-on effects that can really ruin your afternoon.
One: create_function’s arguments are strings, which means they’re invisible to any syntax highlighting, code navigation, or refactoring features you might be using to help make sense of the code. Any attempt to implement these properly for create_function will be heuristic at best.
Two: create_function’s arguments are strings, which means they interact with PHP’s string interpolation rules. The following program has a very subtle bug:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | <?php $axes = array(1, 2, 3, 4); $dimensions = array_map( create_function( "$x", "return $x * $x;" ), $axes ); /* Prints several errors and then nothing (FALSE); should print 1 (TRUE) */ print_r($dimensions == array(1, 4, 9, 16)); ?> |
Can you see it? Here’s the fixed version:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | <?php $axes = array(1, 2, 3, 4); $dimensions = array_map( create_function( '$x', 'return $x * $x;' ), $axes ); /* Prints 1 (TRUE) */ print_r($dimensions == array(1, 4, 9, 16)); ?> |
The PHP developers do think this is a problem: this is one of the motivations for the PHP closures RFC.