I have an array which stores multiple references to a single anonymous function:
$fns = array();
//some code
$fn = function(){
echo 'this is closure 12345... < 67890';
// etc etc..
};
for($x=12345; $x<67890; ++$x){
$fns[$x] = $fn;
}
As can be seen, we're creating only one anonymous function.
What if we put the function declaration inside of the loop? :
$fns = array();
//some code
for($x=12345; $x<67890; ++$x){
$fns[$x] = function(){
echo 'this is closure 12345... < 67890';
// etc etc..
};
}
Is the engine smart enough to recognize that only one object needs to be created?
Does the above code create only one object or does it create one object per iteration?
(Question is targeted at both HHVM and Zend Engine.)