2011-10-28 11 views
0

なぜプログラムが動作していないのかわかりません。私は各生徒のオブジェクトをとり、assign_to_dorm関数を使って寮に追加することになっています。 view_occupants関数を使用して、配列内のすべての占有者を表示することができます。すべてのプログラムは、寮名の1つを反復し、view_occupantsで何もしません。 Assign_to_dorm機能もオブジェクトを配列に追加する - assign_to_dorm関数

を働いている場合、私はindex.phpのファイルさえわからない

<!DOCTYPE html> 
    <html> 
    <head> 
     <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 
     <title></title> 
    </head> 
    <body> 

<?php 

     require_once ('student.class.php'); 
     require_once ('dorm.class.php'); 

     $dorms = array(); 
     $students = array(); 

     $dorms[0]= new Dorm('Landis', 402); 
     $dorms[1]= new Dorm('Salley', 570); 
     $dorms[2]= new Dorm('DeGraff', 700); 
     $dorms[3]= new Dorm('Cawthon', 297); 
     $dorms[4]= new Dorm('Reynolds', 243); 



     $students[0] = new Student('Tim', 'James', 'senior', 'male'); 
     $students[1] = new Student('Kyle', 'McLastly', 'junior', 'male'); 
     $students[2] = new Student('Stacey', 'Keibler','senior','female'); 
     $students[3] = new Student('Jessica', 'Mullins', 'junior','female'); 
     $students[4] = new Student('Kenneth', 'Yagems','senior', 'male'); 
     $students[5] = new Student('Chad', 'Stacey', 'sophomore', 'male'); 
     $students[6] = new Student('Kyle', 'Bridgan', 'senior', 'male'); 
     $students[7] = new Student('Heath', 'Banks', 'sophomore', 'female'); 
     $students[8] = new Student('Christina', 'Burbanks', 'freshman', 'female'); 
     $students[9] = new Student('Thomas', 'Wilson', 'senior', 'male'); 
     $students[10] = new Student('Katy', 'Parks', 'junior', 'female'); 
     $students[11] = new Student('Jay', 'Bradshaw', 'sophomore', 'male'); 
     $students[12] = new Student('Laura', 'Demetri', 'freshman', 'female'); 
     $students[13] = new Student('Bryan', 'Griffin', 'freshman', 'male'); 
     $students[14] = new Student('Stuart', 'Griffin', 'senior', 'male'); 

     shuffle($students); 


     foreach($students as $student){ 

      $key = array_rand($dorms); 
      $dorm = $dorms[$key]; 
      $dorm->assign_to_dorm($student); 

     } 


     foreach ($dorms as $dorm){ 

       echo "<h3>".$dorm->get_dname()."</h3>"; 

       echo "<p>".$dorm->view_occupants()."</p>"; 

     } 

     ?> 
    </body> 
    </html> 

dorm.class.phpファイル

<?php 

     require_once ('student.class.php'); 

    class Dorm 
    { 

    private $dorm_name; 
    private $capacity; 
    private $occupants = array(); 



    public function __construct($dorm_name,$capacity) { 

     $this->dorm_name = $dorm_name; 
     $this->capacity = $capacity; 

    } 


     public function assign_to_dorm($student){ 


      if(count($this->occupants) >= $this->capacity) { 

      return FAlSE; 
      } 

      else{ 

       array_push($this->occupants, $student); 


       return TRUE; 

      } 
     } 

     function get_dname(){ 

      return $this->dorm_name; 
     } 

     function get_capacity(){ 

      return $this->capacity; 
     } 

     function view_occupants(){ 
      foreach($this->occupants as $resident){ 
      echo "<p>".$resident."</p>"; 
      } 
     } 

     } 

    ?> 

答えて

2

あなたのコードは、私にはOKに見えますが、私Dorm::ocupantsStudentオブジェクトの配列であるように見えます。 view_occupants()で反復処理を実行すると、オブジェクトが直接出力されます。 echoの出力を生成するために__toString()というマジックメソッドを呼び出さない限り、意図した情報は表示されません。

私の前提が正しくない場合は、student.class.phpも投稿してください。

function view_occupants(){ 
     foreach($this->occupants as $resident){ 

     // Instead of echoing out the $resident obj, echo out a name: 
     echo "<p>".$resident->lastname."</p>"; 
     } 
    } 
+0

ありがとうございます。私は今日、過去7時間、最後の夜はこのコードを見て5時間を過ごしましたが、その後、あなたは秒で間違ったエラーを発見しました。ほんとうにありがとう!!。それは完全に – webminer07

0

あなたのコードではエラーが発生していないようです。それ以外の場合は、$ residentをオブジェクトとしてエコーするので、の名前などのプロパティの1つをエコーする必要があります。

それがうまくいかない場合は、Studentsクラスのコードも投稿してください。実行するようにしてください。

+0

うまく働いた。それは正確な誤りでした。私はちょうど$ resident-> get_name()を追加し、完璧に機能しました。 – webminer07

関連する問題