首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >回调函数

回调函数
EN

Stack Overflow用户
提问于 2011-05-11 17:06:59
回答 1查看 275关注 0票数 1

对于另一个函数,我有一个回调函数,它循环遍历我博客中一篇文章的评论。我现在尝试切换到“线程/嵌套”注释,因此扩展了回调函数。到目前为止,一切都正常,但我无法摆脱这种感觉,即我没有按照最佳php-实践(和性能)编写它。

我使用一个css框架,并必须对分配给单个注释的.span-xy类做一些计算。我从一个全局常数的输入值开始,然后输出。span-12表示父注释。然后,我必须降低/提高每+/- (int) 1对每级嵌套的值。因此,我开始构建数组,对它们进行计数,迭代并为每个注释构建临时数组。

问题:有更简单的方法来解决这个问题吗?

代码语言:javascript
复制
<!-- 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
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2011-05-11 17:34:32

只是快速扫描后的一些一般性评论。它处理编码,而不考虑功能。

代码语言:javascript
复制
$GLOBALS['comment'] = $comment;

你为什么要把这个放到全球范围?这可以覆盖现有的全局变量。在这里通过引用传递可能更合适。

代码语言:javascript
复制
static $width = MY_GLOBAL_WIDTH_CONSTANT;

为什么这个是静态的?价值是不变的,所以没有必要保留。

代码语言:javascript
复制
if ( $parent > (int) 0 )
[...]
$span_txt = $span - (int) 2;
[...]
$push = $parent_counter != (int) 0 ? 'push-1' : '';

不需要将int文字转换为int。如果您希望进行int比较,则应该将其转换为变量。

代码语言:javascript
复制
if ( ! (array) $ancestors )
    $ancestors = array();

如果为空数组,则为空数组?只需做!isset($ancestors)

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/5967989

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档