Tôi có một phương pháp trích xuất cây chuyển đổi cây preorder đã sửa đổi từ cơ sở dữ liệu và các bộ lọc sử dụng chức năng gọi lại. Ví dụ:Làm cách nào để PHPDoc gọi lại?
/**
* 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;
}
Tôi không chắc chắn những gì PHPDoc nên cho biến $filter
- đó là một cuộc gọi lại, đó là những gì tôi đã chỉ ra, nhưng tôi không chắc chắn nếu đó là chính xác.
Ngoài ra, bất kỳ ý kiến khác về chất lượng (hoặc thiếu) trong mã này sẽ được đánh giá :)
câu hỏi hay. – zerkms