回调函数
问题描述:
我有一个回调函数另一个函数,循环通过我的博客文章的评论。我现在试图切换到“线程/嵌套”注释,因此扩展了回调函数。到目前为止一切正常,但我无法摆脱这种感觉,我没有根据最佳的PHP实践(和性能)编写它。回调函数
我使用一个css框架,并且必须为我分配给单个注释的.span-xy
类执行一些数学运算。我从一个全局常量的输入值开始,例如输出。 span-12
获取父级评论。然后,我必须降低/提高每个嵌套级别的+/- (int) 1
的值。所以我来创建数组,对它们进行计数,迭代并为每个评论构建临时数组。
问题: 有没有更容易的方法来解决这个问题?
<!-- This is the final html mark-up output: List of comments (threaded/nested) -->
<ul>
<li id="1" class="push-<?php echo $push; ?> span-<?php echo $span; ?>">comment - parent</li>
<li id="2">comment - child of #1
<ul class="children">
<li id="3">comment - child of #2
<li id="4">comment - child of #2
<ul class="children">
<li id="5">comment - child of #4</li>
</ul>
<li id="6">comment - child of #2</li>
</ul>
<li id="7">comment - child of #2
<ul class="children">
<li id="8">comment - child of #7</li>
<li id="9">comment - child of #7</li>
</ul>
</li>
</li>
</ul>
<?php
// This is my callback function
function comment_list_cb($comment, $args, $depth)
{
// retrieve the data from the globals or make them available
$GLOBALS['comment'] = $comment;
global $post;
static $width = MY_GLOBAL_WIDTH_CONSTANT;
static $ancestors = null;
// is Child/Parent comment
$parent = (int) $comment->comment_parent; // retrieve the ID of the parent
$is_child = false;
if ($parent > (int) 0) // if we got a parent
{
$is_child = true;
if (! (array) $ancestors)
$ancestors = array();
if (! array_key_exists($parent, $ancestors))
{
$ancestors[$parent] = get_comment_ID();
}
else
{
foreach ($ancestors as $parent_id => $child_id)
{
if ($parent_id == $parent)
{
$ancestors_temp[$parent_id] = $child_id;
break;
}
$ancestors_temp[$parent_id] = $child_id;
}
$ancestors = $ancestors_temp;
}
$parent_counter = count($ancestors);
$span = $width - (int) $parent_counter;
}
else
{
$ancestors = $parent_counter = null;
$span = MY_GLOBAL_WIDTH_CONSTANT;
}
$span_txt = $span - (int) 2; // reduce per `2` because of the span-2 class at the avatar element
// now: build the classes
$push = $parent_counter != (int) 0 ? 'push-1' : '';
$child = $is_child === true ? ' child ' : '';
$list = comment_class('span-'.$span.' '.$push.' append-bottom last hreview comment-'.get_comment_ID().' '.$microid, get_comment_ID(), $post->ID, false);
?>
<!-- build the comment -->
<li <?php echo $list; ?>>
<div id="<?php get_comment_ID(); ?>">
<span class="comment-avatar span-2"><!-- display avatar img - width is span-2 --></span>
<span class="comment-meta span-<?php echo $span_txt; ?> last"><!-- display meta data like timestamp, etc. --></span>
<span class="comment-text span-<?php echo $span_txt; ?> last"><!-- display message --></span>
</div>
</li>
<?php
}
答
快速扫描后只是一些一般的意见。它处理编码,而不管功能如何。
$GLOBALS['comment'] = $comment;
你为什么把这个放在全球范围内?这可以覆盖现有的全局变量。通过引用传递在这里可能更合适。
static $width = MY_GLOBAL_WIDTH_CONSTANT;
为什么这是静态的?价值从不改变,所以没有必要保留。
if ($parent > (int) 0)
[...]
$span_txt = $span - (int) 2;
[...]
$push = $parent_counter != (int) 0 ? 'push-1' : '';
无需将int文字转换为int。如果你想int比较,那就是你应该投射的变量。
if (! (array) $ancestors)
$ancestors = array();
如果为空数组,请使空数组?只是做!isset($ancestors)
英语不是我的母语。什么是“不需要将int literal转换为int。如果你想要int比较,那就是你应该投射的变量。”意思?顺便说一句:我解决了前两件事(从一些尝试/错误的东西遗留下来)。最后一个......是的,你是对的。 – kaiser 2011-05-11 18:10:04
你不需要'(int)2',因为2已经保证是一个int。 – webbiedave 2011-05-11 18:18:47