php全局变量和实例变量利用率

时间:2014-02-26 13:15:40

标签: php

我很难完成一项简单的任务。我有一个方法可以生成随机数,并根据结果为数组变量分配特定的结果。我想要做的是通过实例方法获取该数组变量,该方法将从另一个类调用。

   <?php

   class MyClass
   {
         public $results = array(array());

         public function simulated_games()
         {
             $game_series1=array(23,34,56);
             $game_series2=array(31,42,67);

             $iter_wins=array(array());

             for($i=0; $i<10;$i++)
             {
                 $random = rand(0,100);
                 for($b=0; $b<1;$b++)
                 {

                   if($random <= $game_series1[0])
                   {
                        $iter_wins[$i][$b]=3;
                   }

                   else if($random <= $game_series2[0]+$game_series2[1])
                   {
                        $iter_wins[$i][$b]=1;
                   }
                 }
             }

            $results=$iter_wins;
        }

        >here i create method just to return variable
        public function get_simulated_games()
        {
          return $this->results;
        }
   }



  <?php

  $a= new MyClass();
  $a->simulated_games();

  $array = array();
  >here is the issue, it return just 1, but supposed to return range numbers
  $array=$a->get_simulated_games();

  for($f=0; $f<sizeof($array);$f++)
  {
    for($g=0; $g<5;$g++)
    {
        echo $array[$f][$g];
    }
        echo '<br>';
  }

  ?>

1 个答案:

答案 0 :(得分:0)

结果中有错误。

修改未设置的内部函数变量而不是类变量

更改

$results=$iter_wins;

$this->results=$iter_wins;