【问题标题】:add object to array - assign_to_dorm function将对象添加到数组 - assign_to_dorm 函数
【发布时间】:2011-10-28 20:26:29
【问题描述】:

我无法弄清楚程序为什么不工作。我应该获取每个学生对象并使用 assign_to_dorm 函数并将它们添加到宿舍。然后使用 view_occupants 函数打印出数组中的所有占用者。程序所做的只是迭代 Dorm 名称之一,而对 view_occupants 不做任何事情。我什至不确定 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>";
          }
        }

       }

  ?>

【问题讨论】:

    标签: php arrays class function object


    【解决方案1】:

    您的代码在我看来没问题,但我注意到Dorm::ocupants 似乎是Student 对象的数组。当您在view_occupants() 中对其进行迭代时,您将直接回显该对象。除非它调用 __toString() 魔术方法来为 echo 生成输出,否则您将不会显示您想要的信息。

    如果我的假设不正确,也请发帖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>";
          }
        }
    

    【讨论】:

    • 非常感谢。我今天花了 7 个小时,昨晚花了 5 个小时盯着这段代码,但是你很快就发现了愚蠢的错误。对此,我真的非常感激!!。它工作得很好
    【解决方案2】:

    嗯,我似乎无法在您的代码中发现任何错误。除此之外,您回显 $resident 这是一个对象,因此您必须回显它的一个属性,例如 name

    试试吧,如果它不起作用,也发布 Student 类的代码,以便我可以尝试运行它。

    【讨论】:

    • 是的。那是确切的错误。我刚刚添加了 $resident->get_name() 并且它完美地工作。
    猜你喜欢
    • 2021-01-13
    • 2022-01-13
    • 2018-09-06
    • 1970-01-01
    • 1970-01-01
    • 2014-04-22
    • 1970-01-01
    相关资源
    最近更新 更多