T
T
Thores2015-02-26 09:50:04
PHP
Thores, 2015-02-26 09:50:04

Conflict of identical functions. How to fix?

Guys help. There is a declension function

<?php defined('JPATH_BASE') or die; 
$comments = JPATH_SITE . '/components/com_jcomments/jcomments.php';
  if (file_exists($comments)) {
    require_once($comments);
    $options = array();
    $options['object_id'] = $id;
    $options['object_group'] = 'com_content';
    $options['published'] = 1;
    $count = JCommentsModel::getCommentsCount($options);
/* Cклонение числительных */	
  $form1 = JText::_('INVIP_PREVIEW_COMMENT');
  $form2 = JText::_('INVIP_PREVIEW_COMMENTS');
  $form3 = JText::_('INVIP_PREVIEW_COMMENTSS');
  $form4 = JText::_('INVIP_PREVIEW_NONE_COMMENTS');
  function format_by_count($count, $form1, $form2, $form3){
    $count = abs($count) % 100;
    $lcount = $count % 10;
    if ($count >= 11 && $count <= 19) return($form3);
    if ($lcount >= 2 && $lcount <= 4) return($form2);
    if ($lcount == 1) return($form1);
    return $form3;
  }
  $counts = $count ? ($count . ' ' .  format_by_count($count, $form1, $form2, $form3)) : $form4;
}
?>
<li class="attr comments">
  <meta itemprop="commentsUserCount" content="<?php echo $count; ?> UserComments" />
  <?php echo $counts; ?>
</li>

First, there is a request for the number of comments. Then comes the function itself and the output. And everything works when we admit one article. and when two it turns out that function requests again and the error turns out. Please help, how can I make sure that there is no conflict. I'm not an expert, but I know that everything is quite simple here. However, I couldn't solve the problem myself.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry Entelis, 2015-02-26
@Thores

0) "an error is obtained" - a bad description of the problem. At a minimum, you need to write the text of the error and the line.
1) I can assume in telepath mode that the file you provided is called in a loop for each article.
Accordingly, you get the 2nd definition of the format_by_count function.
In general, it is better never to define a function in a conditional block, but to define it somewhere higher.
The problem can be solved locally

if (!function_exists("format_by_count")) {
  function format_by_count($count, $form1, $form2, $form3){
    $count = abs($count) % 100;
    $lcount = $count % 10;
    if ($count >= 11 && $count <= 19) return($form3);
    if ($lcount >= 2 && $lcount <= 4) return($form2);
    if ($lcount == 1) return($form1);
    return $form3;
  }
}
but it's still shitty code.
2) Writing html layout and php logic in one file is a bad form. Read about some mvc.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question