English 中文(简体)
增加阵列的反对——分配_to_dorm函数
原标题:add object to array - assign_to_dorm function

我不能说明为什么该方案没有奏效。 我应带每个学生的物体,并用他们来分配_to_dorm,并在护堤上添加。 然后,在阵列中印刷所有占用者,以图人功能。 所有方案都是假名之一的,与占领者的观点无关。 我甚至不敢肯定,阿贝托--护堤功能甚至正在发挥作用。

指数。 网址

    <!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>";
          }
        }

       }

  ?>
最佳回答

你的代码看着我,但我注意到<代码>:占用者似乎是<代码>的系列物体。 当你在<代码>view_occupants()上填满时,你将直接重复标语。 Unless it calls a __toString(> magic methods to production for echo, Younking t at showing the information used.

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>";
      }
    }
问题回答

我似乎无法发现你法典中的任何错误。 除此以外,你还照搬了作为物体的$ /strong>,因此,你必须重复其中的一种特性,如

Try it out, if it doesn t work post the code for the Students class as well so I can try running it.





相关问题
Brute-force/DoS prevention in PHP [closed]

I am trying to write a script to prevent brute-force login attempts in a website I m building. The logic goes something like this: User sends login information. Check if username and password is ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

定值美元

如何确认来自正确来源的数字。

Generating a drop down list of timezones with PHP

Most sites need some way to show the dates on the site in the users preferred timezone. Below are two lists that I found and then one method using the built in PHP DateTime class in PHP 5. I need ...

Text as watermarking in PHP

I want to create text as a watermark for an image. the water mark should have the following properties front: Impact color: white opacity: 31% Font style: regular, bold Bevel and Emboss size: 30 ...

How does php cast boolean variables?

How does php cast boolean variables? I was trying to save a boolean value to an array: $result["Users"]["is_login"] = true; but when I use debug the is_login value is blank. and when I do ...