Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How should I PHPDoc a callback?

Tags:

php

phpdoc

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 :)

like image 698
Billy ONeal Avatar asked Feb 07 '11 07:02

Billy ONeal


People also ask

What is a callback and how do you use it?

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.

What is the point of a callback?

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.

Why callback is called callback?

A Callback is a function that is to be executed after another function has finished executing — hence the name 'call back'.

What is a callback class?

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.


1 Answers

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.

like image 55
mindplay.dk Avatar answered Oct 09 '22 02:10

mindplay.dk