1

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.

Hephaestus
  • 1,982
  • 3
  • 27
  • 35
  • Although pretty ugly `create_function` is still supported so you can just use that no matter the PHP version. However if it was up to me I would just drop PHP 5.2 support. – PeeHaa Dec 26 '12 at 05:04
  • @PeeHaa I will see what I can do about getting you named product manager... ;-) – Hephaestus Dec 26 '12 at 05:06

0 Answers0