duanfen1312 2011-10-28 20:26
浏览 61
已采纳

将对象添加到数组 - assign_to_dorm函数

I cannot figure out why the program is not working. I am supposed to take each student object and use to assign_to_dorm function and add them to the dorm. and then print out all of the occupants in the array with the view_occupants function. All the program does is iterate one of the Dorm names and does nothing with the view_occupants. I'm not even sure if the Assign_to_dorm function is even working

index.php file

    <!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 file

<?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条回答 默认 最新

  • dssqq82402 2011-10-28 20:37
    关注

    Your code looks ok to me, but I notice that Dorm::ocupants appears to be an array of Student objects. When you iterate over it in view_occupants(), you are echoing out the object directly. Unless it calls a __toString() magic method to produce output for echo, you won't be displaying the information you intend.

    If my assumption is incorrect, please also post 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>";
          }
        }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?