2010-04-09 99 views
0

我在與變量的wordpress的functions.php

 $prev_dept = 0; 
     $comment_count = 0; 
     $comment_index = 0; 
     function setCommentCount($size){ 
      $comment_count = $size; 
     }   
     function flowhub_comment($comment, $args, $depth) { 
      $comment_index ++;    

      if($depth > 1) { 
       $line = true; 
      } 
      echo '$prev_dept:' . $prev_dept.'<br>'; 
     } 

的functions.php文件的問題我不能訪問$ comment_index所以我不能設置也從一個函數中得到它。我應該怎麼做才能解決這個問題?

此致如實

回答

2

$comment_index不是的職責範圍內,你需要使用globalMore details on scoping in PHP

+0

謝謝,我認爲這很奇怪,因爲通常我使用私人,公共等......而在其他語言中,當您在課堂上工作時可能會獲得訪問權限。謝謝我會盡可能地標記你的答案(11分鐘) – Ayrton 2010-04-09 17:04:38

+0

如果你對課程感到滿意,你可以在PHP中使用它們。然後你可以將這些變量放在類的範圍內,然後使它可以通過類方法訪問。 http://us.php.net/manual/en/language.oop5.basic.php – unholysampler 2010-04-09 17:14:34

1

function.php的工作方式不僅僅是一個普通的include,嘗試GLOBAL可能會有所幫助。

function setCommentCount($size){ 
    global $comment_count; 
    $comment_count = $size; 
}