Re: [PATCH] [RFC] Closures and lambda functions in PHP
| From: | Alexander Wagner | Date: | Thu, 26 Jun 2008 20:46:23 +0000 |
| Subject: | Re: [PATCH] [RFC] Closures and lambda functions in PHP | ||
| References: | 1 2 3 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-38648@lists.php.net to get a copy of this message | ||
> * I added tests (Zend/tests/closures_*.phpt) that ensure the correct
> behaviour of closures.
I'd like to propose an additional test to ensure closures can all themselves:
<?php
$i = 3;
$lambda = function ($lambda) use ($i) {
if ($i==0) return;
echo $i--."\n";
$lambda($lambda);
};
$lambda($lambda);
echo "$i\n";
?>
Expected output:
3
2
1
3
I see exactly one problem with the patch, which is that the above script
shouldn't work without "use (&$i)".
I find it counterintuitive that the creation of the lambda creates a copy of
$i, but all invocations of $lambda use a reference to the same $i.
For n calls to $lambda, there are only 2 copies of $i (one global, one static
in $lambda) where I would expect n+1 copies.
Gesundheit
Wag
--
Sieh dich, nimm Sorge.