erstellt am 2. März 2015 in dev | kommentieren

Recently, I just encountered some strange behavior when I converted (very!) old PHP code for a friend. I was rewriting the code on a machine using PHP 5.4. and 5.6., so I just added a dynamic function call like the following one using the preg_replace_callback function.

After running the script on a production machine using PHP 5.2.16, there was an error finding the function tmpl_evaluate(). Like following

Parse error: syntax error, unexpected T_FUNCTION…

After doing some googling and reading the manual, I figured out that the syntax for the callback I chose was added to PHP 5.3. To put it another way: in versions of PHP prior to 5.3, native anonymous functions/closures are not available. In earlier versions one has to use create_function or create a static class function if there is no need to access variables in that class. Also, only from PHP 5.3 you can use an anonymous functions to pass local variables into the callback. I already had the function tmpl_evaluate, so I simply fixed this by calling the static function as described in the manual with #example 2.