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. * * @static * @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) { return true; }; } $result = array(); $childrenCount = 0; for ($idx = 0; $idx < count($rows); $idx += $childrenCount + 1) { $current = $rows[$idx]; $childrenCount = self::ChildrenCountFromRow($current); if (!$filter($current)) { continue; } $childrenStartAt = $idx + 1; $childRows = array_slice($rows, $childrenStartAt, $childrenCount); $children = self::MakeTreeGivenDbRows($childRows, $filter); $result[] = new Cas_Template_TreeNode(self::MakeNodeGivenDbRow($current), $children); } if (empty($result)) { return null; } return $result; }
I'm not sure what the PHPDoc should be for the variable $filter
-- it's a callback, which is what I've indicated, but I'm not sure if that's correct.
Also, any other comments on the quality (or lack thereof) in this code would be appreciated :)
A callback function is a function passed into another function as an argument, which is then invoked inside the outer function to complete some kind of routine or action. The above example is a synchronous callback, as it is executed immediately.
Callbacks make sure that a function is not going to run before a task is completed but will run right after the task has completed. It helps us develop asynchronous JavaScript code and keeps us safe from problems and errors.
A Callback is a function that is to be executed after another function has finished executing — hence the name 'call back'.
In the simplest terms a callback is a code that you pass into another method. E.g. you have a class A which calls a method on class B but you need some code running from Class A when it's finished. You put your code in its own new method on class A and you pass the method name in when you call the method on class B.
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)
Note that 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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With