php - Callback function -


i have callback function function loops through comments of article in blog. tried switch "threaded/nested" comments , therefore extended callback function. far works, can't rid of feeling, didn't write according best php-practice (and performance).

i use css framework , have math .span-xy classes assign single comments. start input value global constant , output eg. span-12 parent comment. have reduce/rise value per +/- (int) 1 every level of nesting. came building arrays, counting them, iterating through , building temp arrays every comment.

question: there easier way around this?

<!-- 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 // callback function function comment_list_cb( $comment, $args, $depth ) {     // retrieve data globals or make them available     $globals['comment'] = $comment;     global $post;      static $width = my_global_width_constant;     static $ancestors = null;      // child/parent comment     $parent = (int) $comment->comment_parent; // retrieve id of parent      $is_child = false;     if ( $parent > (int) 0 ) // if got parent     {         $is_child = true;          if ( ! (array) $ancestors )             $ancestors = array();          if ( ! array_key_exists( $parent, $ancestors ) )         {             $ancestors[$parent] = get_comment_id();         }         else          {             foreach ( $ancestors $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 span-2 class @ avatar element      // now: build 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 comment -->     <li <?php echo $list; ?>>         <div id="<?php get_comment_id(); ?>">             <span class="comment-avatar span-2"><!-- display avatar img - width span-2 --></span>             <span class="comment-meta span-<?php echo $span_txt; ?> last"><!-- display meta data timestamp, etc. --></span>             <span class="comment-text span-<?php echo $span_txt; ?> last"><!-- display message --></span>         </div>     </li>     <?php } 

just general comments after quick scan. deals coding, irrespective of functionality.

$globals['comment'] = $comment; 

why putting in global scope? can overwrite existing global variable. passing reference may more appropriate here.

static $width = my_global_width_constant; 

why static? value never changed there's no need retain.

if ( $parent > (int) 0 ) [...] $span_txt = $span - (int) 2; [...] $push = $parent_counter != (int) 0 ? 'push-1' : ''; 

no need cast int literal int. if want int comparison, it's variable should casting.

if ( ! (array) $ancestors )     $ancestors = array(); 

if empty array, make empty array? !isset($ancestors)


Comments

Popular posts from this blog

c# - how to write client side events functions for the combobox items -

exception - Python, pyPdf OCR error: pyPdf.utils.PdfReadError: EOF marker not found -