2014-02-26 27 views
0

我很难完成一项简单的任务。我有一个方法会产生随机数,并根据结果为数组变量赋予特定的结果。我想要做的是通过实例方法获取该数组变量,该方法将从另一个类中调用。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>'; 
    } 

    ?> 
+0

'simulated_games()'正在修改局部变量,而不是实例一个 –

+1

''random','$ game_series1'和'$ game_series2'没有在'simulation_games'方法中定义。 –

+0

'simulated_games()'似乎没有做任何事情,除非生成未定义的变量警告。 – jeroen

回答

0

您在结果中出现错误。

您修改INTERAL函数变量,没有设置的,而不是类变量

变化

$results=$iter_wins; 

$this->results=$iter_wins; 
+0

谢谢你的回答 – user2925006