dorpbn1027 2012-08-17 22:23
浏览 45
已采纳

PHP类 - 公共变量在类外没有正确显示

In myClass I have declared $_total_results_found = 0

Then in my function I changed the value of $_total_results_found = 10

But, when I try to call this outside myClass the value still shows me 0.

Please can any one help me how to write this code?

class myClass
{
  private $_total_rows_count = 0;
  public function foundResults()
  {
    $count = 10;
    $this->_total_rows_count = $count;
  }
} // end myClass


$myclass = new myClass();
echo $myclass->_total_results_found; // Value is showing 0 instead of 10
  • 写回答

2条回答 默认 最新

  • doulanyan6455 2012-08-17 22:31
    关注

    Here you go:

    <?
    class myClass
    {
      public $_total_rows_count = 0;
      public function foundResults()
      {
        $count = 10;
        $this->_total_rows_count = $count;
      }
    } // end myClass
    
    
    $myclass = new myClass();
    $myclass->foundResults();
    echo $myclass->_total_rows_count; //will print 10
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?