The following fails with a parse error on PHP 5.2.X:
if (version_compare(PHP_VERSION, '5.3.0') >= 0) {
$greater = function($left, $right) {
return $left > $right;
}
}
else {
$greater = create_function('$left, $right', 'return $left > $right;');
}
I guess the reason is probably rather obvious, so the real question is:
"Is there some way for me to do this without putting the 5.3.0-dependent definition in a separate file, and then conditionally including the file if the version is 5.3.0 or higher?"
If the function I wanted to define was a first-class named function, I'd have fewer compunctions about putting it in a separate file. But from a code-readability standpoint (IMO), one of the benefits of an anonymous function is the fact that its source text is visible in the body of the module that defines/uses it. Putting the definition off in a different source file pretty much kills that benefit.