Recursive Closures in PHP

"kind"
/
"type"
/ | \
widget widget widget
graph
var widgets = [...arrayOfWidgets];
var remove = function(widgetId) {
widgets[widgetId].getChildWidgetIds().forEach(function(childWidgetId) {
// `remove` is defined and accessible
// at the time this is called
remove(widgets[childWidgetId]);
});
delete widgets[$pageId];
};
remove(widgets[pageId]);
recursion.js
$widgets = [...arrayOfWidgets];
$remove = function($widgetId) use (&$widgets, &$remove) {
foreach ($widgets[$widgetId]->getChildWidgetIds() as $childWidgetId) {
$remove($widgets[$childWidgetId]);
}
unset($widgets[$widgetId]);
};
$remove($widgets[widgetId]);
recursion.php