我创建了两个对象类:Student类和Dorm类。在宿舍类中,我创建了一个名为assign_to_dorm的方法,该方法应该计算宿舍中当前占用者的数量,如果等于或高于容量,则应返回false。如果低于capcity,则该方法将Student对象添加到Dorm类的占用者数组属性中。
这是我的宿舍课
<?php
class Dorm
{
private $dorm_name;
private $capacity;
private $occupants = array();
public function assign_to_dorm($student)
{
$ammount = count($this->occupants);
if($ammount >= $this->capacity)
{
return FALSE;
}else{
array_push($occupants, $student);
}
}
public function set_dorm_name($dorm_name)
{
$this->dorm_name = $dorm_name;
}
public function set_capacity($capacity)
{
$this->capacity = $capacity;
}
public function get_dorm_name()
{
return $this->dorm_name;
}
public function get_capacity()
{
return $this->capacity;
}
public function view_occupants()
{
foreach($this->occupants as $resident)
{
echo "<br/>" . $resident;
}
}
public function __construct($dorm_name,$capacity)
{
$this->set_dorm_name($dorm_name);
$this->set_capacity($capacity);
}
}
?>
在我的网站索引文件中,我创建了一个$ dorms数组和一个$ students数组,我将用它来测试该方法。然后我将学生列表拖沓,最后我让学生循环,这样就可以使用assign_to_dorm()方法将它们添加到随机宿舍中。之后我希望它打印出宿舍里的居住者。
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Assignment 3</title>
</head>
<body>
<?php
require('student.php');
require('dorm.php');
$dorms = array();
$dorms[] = new Dorm("Lindo Hall", 50);
$dorms[] = new Dorm("Smith Hall", 20);
$dorms[] = new Dorm("Salley Hall", 200);
$students = array();
$students[] = new Student("Mike", "Lindo", "Senior", "Male");
$students[] = new Student("Miguel", "Pakesh", "Freshman", "Male");
$students[] = new Student("Sammie", "Maxwell", "Sophomore", "Female");
$students[] = new Student("John", "Smith", "Junior", "Male");
$students[] = new Student("Jane", "Doe", "Sophomore", "Female");
$students[] = new Student("John", "Smith", "Senior", "Male");
$students[] = new Student("Jane", "Doe", "Junior", "Female");
$students[] = new Student("John", "Smith", "Freshman", "Male");
$students[] = new Student("Jane", "Doe", "Freshman", "Female");
$students[] = new Student("John", "Smith", "Sophomore", "Male");
$students[] = new Student("Jane", "Doe", "Senior", "Female");
$students[] = new Student("Chris", "Doe", "Freshman", "Male");
$students[] = new Student("Sarah", "Smith", "Sophomore", "Female");
$students[] = new Student("Chris", "Doe", "Junior", "Male");
$students[] = new Student("Sarah", "Smith", "Senior", "Female");
shuffle($students);
foreach($students as $student)
{
$dorm =& array_rand($dorms); // LINE 47
$dorm->assign_to_dorm($student); // LINE 48
}
echo "<table><tbody>";
foreach($dorms as $dorm)
{
echo "<tr>";
echo "<td>" . $dorm->view_occupants() . "</td>";
echo "</tr>";
}
?>
</body>
</html>
我每次运行网站时都会收到这些错误:
严格标准:只应在第47行的C:\ Program Files \ EasyPHP-5.3.8.0 \ www \ assignment3 \ index.php中通过引用分配变量
致命错误:在第48行的C:\ Program Files \ EasyPHP-5.3.8.0 \ www \ assignment3 \ index.php中的非对象上调用成员函数assign_to_dorm()
任何想法我可能做错了什么?我想在assign_to_dorm()方法中的代码一定有问题,或者我仍然需要一个占用者数组的构造函数,我不认为这是问题。任何帮助是极大的赞赏。非常喜欢
答案 0 :(得分:3)
现代PHP版本默认将returnin对象作为引用,因此=&
赋值不是必需的。这是第一个警告。
第二个警告:array_rand返回一个数组KEY,而不是一个数组VALUE。所以你得到像5
这样的东西,而不是你分配给阵列的第五个对象。
代码应为
$dorm_index = array_rand($dorms); // LINE 47
$dorms[$dorm_index]->assign_to_dorm($student); // LINE 48
答案 1 :(得分:2)
mixed array_rand ( array $input [, int $num_req = 1 ] )
Picks one or more random entries out of an array, and returns
the key (or keys) of the random entries.
它选择了关键......你需要说
$key = array_rand($dorms);
$dorm = $dorms[$key];
答案 2 :(得分:0)
array_Rand
不返回元素,它返回键。然后你需要获得元素:
$key = array_rand($dorms); // LINE 47
$dorm = $dorms[$key];
$dorm->assign_to_dorm($student);