Watch out when 'importing' variables to a closure's scope -- it's easy to miss / forget that they are actually being *copied* into the closure's scope, rather than just being made available.
So you will need to explicitly pass them in by reference if your closure cares about their contents over time:
<?php
$result = 0;
$one = function()
{ var_dump($result); };
$two = function() use ($result)
{ var_dump($result); };
$three = function() use (&$result)
{ var_dump($result); };
$result++;
$one(); $two(); $three(); ?>
Another less trivial example with objects (what I actually tripped up on):
<?php
$myInstance = null;
$broken = function() uses ($myInstance)
{
if(!empty($myInstance)) $myInstance->doSomething();
};
$working = function() uses (&$myInstance)
{
if(!empty($myInstance)) $myInstance->doSomething();
}
if(SomeBusinessLogic::worked() == true)
{
$myInstance = new myClass();
}
$broken(); $working(); ?>