php代码行中出现致命错误
我创建了两个对象类:一个学生类和一个宿舍类。在dorm类中,我创建了一个名为assign_to_dorm的方法,该方法用于计算宿舍中当前居住者的数量,如果等于或高于容量,则应返回false。如果低于capcity,则该方法将向Dorm类的Occumbers数组属性中添加一个Student对象 这是我的宿舍班php代码行中出现致命错误,php,html,Php,Html,我创建了两个对象类:一个学生类和一个宿舍类。在dorm类中,我创建了一个名为assign_to_dorm的方法,该方法用于计算宿舍中当前居住者的数量,如果等于或高于容量,则应返回false。如果低于capcity,则该方法将向Dorm类的Occumbers数组属性中添加一个Student对象 这是我的宿舍班 <?php class Dorm { private $dorm_name; private $capacity; p
<?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>
作业3
每次运行站点时,我都会收到这些错误:
严格标准:在第47行的C:\Program Files\EasyPHP-5.3.8.0\www\assignment3\index.php中,只能通过引用来分配变量
致命错误:在C:\Program Files\EasyPHP-5.3.8.0\www\assignment3\index.php的第48行对非对象调用成员函数assign_to_dorm()
你知道我做错了什么吗?我猜assign_to_dorm()方法中的代码一定有问题,或者我仍然需要一个用于占用者数组的构造函数,我认为这不是问题所在。非常感谢您的帮助。很多爱来自
它选择了钥匙。。。你需要说
$key = array_rand($dorms);
$dorm = $dorms[$key];
从
它选择了钥匙。。。你需要说
$key = array_rand($dorms);
$dorm = $dorms[$key];
现代PHP版本默认将对象作为引用返回,因此不需要
=&
赋值。这是第一个警告
第二个警告:array_rand返回数组键,而不是数组值。因此,您将返回类似于5
,而不是指定给数组的第5个对象
代码应该是
$dorm_index = array_rand($dorms); // LINE 47
$dorms[$dorm_index]->assign_to_dorm($student); // LINE 48
现代PHP版本默认将对象作为引用返回,因此不需要
=&
赋值。这是第一个警告
第二个警告:array_rand返回数组键,而不是数组值。因此,您将返回类似于5
,而不是指定给数组的第5个对象
代码应该是
$dorm_index = array_rand($dorms); // LINE 47
$dorms[$dorm_index]->assign_to_dorm($student); // LINE 48
array\u Rand
不返回元素,而是返回键。然后需要获取元素:
$key = array_rand($dorms); // LINE 47
$dorm = $dorms[$key];
$dorm->assign_to_dorm($student);
array\u Rand
不返回元素,而是返回键。然后需要获取元素:
$key = array_rand($dorms); // LINE 47
$dorm = $dorms[$key];
$dorm->assign_to_dorm($student);
这与Java有什么关系?student类只包括4个私有属性(名字、姓氏、级别、性别)、其getter和setter以及所有属性的构造函数。这与Java有什么关系?student类只包括4个私有属性(名字、姓氏、级别、性别)、getter和setter,和所有属性的构造函数。这很有效!非常感谢,现在我一直得到的是Dorm类中代码行的错误警告:array_push()期望参数1为array,在C:\Program Files\EasyPHP-5.3.8.0\www\assignment3\Dorm.php的第23行公共函数assign_to_Dorm($student){$amount=count($this->accupermissions)中给出的null;如果($amount>=$this->capacity){return FALSE;}否则{array_push($accumbers,$student);}您在array\u push呼叫中忘记了您的住户的
$this->
。成功了!非常感谢,现在我一直得到的是宿舍类代码行的错误警告:array\u push()要求参数1为数组,在第23行的C:\Program Files\EasyHP-5.3.8.0\www\assignment3\dorm.php中为空,公共函数assign_to_dorm($student){$amount=count($this->accumpors);if($amount>=$this->capacity){return FALSE;}否则{array\u($accumbers,$student);}}您忘记了在数组的push调用中使用$this->
。