I have a method which extracts a modified preorder tree transversal tree from the database, and filters that using a callback function. For example:
* Recursive function for building the Cas_Template_TreeNode.
* @param array $rows
* @param callback $filter A function to filter the tree by (return a value convertible to false to remove the item from the tree)
* @return array
private static function MakeTreeGivenDbRows($rows, $filter = null)
if ($filter === null)
$filter = function($unused)
$result = array();
$childrenCount = 0;
for ($idx = 0; $idx < count($rows); $idx += $childrenCount + 1)
$current = $rows[$idx];
$childrenCount = self::ChildrenCountFromRow($current);
$childrenStartAt = $idx + 1;
$childRows = array_slice($rows, $childrenStartAt, $childrenCount);
$children = self::MakeTreeGivenDbRows($childRows, $filter);
$result = new Cas_Template_TreeNode(self::MakeNodeGivenDbRow($current), $children);
The correct type-hint is
callable, which has been available in e.g PhpStorm for a long time, and is part of PSR-5 which is currently under specification.
(I'm surprised nobody else mentioned
callable, it's nothing new and has been used everywhere for years - to my knowledge,
callback is not and never was a defined PHP pseudo-type)
callable includes not just closures, but also "old school" PHP callbacks, e.g.
array($object, 'methodName') or
array('ClassName', 'methodName') and even
'function_name'- to maximize the usefulness of your API, you should cover all of those use-cases, which is quite easy, since both call_user_func and call_user_func_array support all four varieties of callables: function-name as string, object/method-name, class/method-name and closure.