2014-02-26 3 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'는'simulated_games' 메소드에 정의되어 있지 않습니다. –

+0

'simulated_games()'는 정의되지 않은 변수 경고를 생성하는 것 외에는 아무 것도하지 않는 것처럼 보입니다. – jeroen

답변

0

결과가 잘못되었습니다.

당신은 interal 대신에 변수 클래스

변화

$results=$iter_wins; 

$this->results=$iter_wins; 
+0

대답 해줘서 고마워. – user2925006