Re: PATCH: anonymous functions in PHP
| From: | Sean Coates | Date: | Mon, 19 Mar 2007 19:25:10 +0000 |
| Subject: | Re: PATCH: anonymous functions in PHP | ||
| References: | 1 2 3 4 5 6 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-28434@lists.php.net to get a copy of this message | ||
> If it would create anonymous function compile-time, it would be a big
> advantage to Wez's patch because then this function could be cached.
> Thinking about this, maybe it is the reason enough to do this even if
> it's not real closure.
On mulling this over a bit more, other than quick one-off callbacks
(which would definitely benefit from avoiding compile on every request),
one of the key reasons to use create_function() is actually to create
dynamic functions:
$fancyVer = create_function('', 'return "PHP " . phpversion();');
// could be optimized as:
$fancyVer = create_function('', 'return "PHP ' . phpversion()
.'";');
(the latter only calls phpversion() at declaration, not each time the
lambda runs)
Since phpversion() is available globally, this isn't a problem. But what
happens if we want to use a variable, instead?
$ver = phpversion();
$fancyVer = create_function('', "return 'PHP $ver';");
// this currently works ^
How would this be rewritten, though?
$ver = phpversion();
$fancyVer = function () { return "PHP $ver"; };
where would $ver come from? the parent scope? the lambda's local scope?
what if it's defined in both places?
S