PHP: Unset all defined variables
Published:
foreach(get_defined_vars() as $k => $v)
unset($$k);
unset($k, $v);
For example handy in this setting:
foreach($iterable as $item)
{
extract($item);
unset($item);
// Define another variable, for only some of the items
if($foo == 'bar')
$x = 2;
// Yield all defined vars
yield get_defined_vars();
// Cleanup, to prevent $x and other variables from
// sticking around to the next iteration
foreach(get_defined_vars() as $k => $v)
unset($$k);
unset($k, $v);
}