在 PHP 函数的最后一个括号中不断出现语法错误
我正在尝试为我创建的 Dorm 类编写一个方法,该方法是 allocate_to_dorm(),它接受单个参数,即学生对象。
该方法应该做两件事:
检查 habitants 属性数组中的项目数是否等于或大于容量属性。如果是,则返回 FALSE。 如果没有,则获取学生对象,并将其作为项目添加到占用者属性数组中。
<?php
class Dorm
{
private $dorm_name;
private $capacity;
private $occupants = array();
public function assign_to_dorm(Student)
{
$ammount = count($occupants);
if($ammount >= $capacity)
{
return FALSE;
}
} // <--------------- KEEP GETTING ERRORS HERE
}
?>
I'm trying to write a method for a Dorm class I created, the method is assign_to_dorm(), which accepts a single argument, which is a student object.
This method should do two things:
Check to see if the number of items in the occupants property array is equal to or greater than the capacity property. If so, return FALSE.
If not, take the student object, and add it as an item in the occupants property array.
<?php
class Dorm
{
private $dorm_name;
private $capacity;
private $occupants = array();
public function assign_to_dorm(Student)
{
$ammount = count($occupants);
if($ammount >= $capacity)
{
return FALSE;
}
} // <--------------- KEEP GETTING ERRORS HERE
}
?>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
你的参数是一个裸字,不起作用。它应该是变量和/或变量+类型提示。
Your parameter is a bare word, which does not work. It should be a variable and/or variable + type hint.
您的函数定义中有 2 个错误:
您传递给函数的参数应该是
$student
类成员应使用
$this
进行限定。$ocupants
是在函数作用域中定义的局部变量,$this->ocupants
是类成员。You have 2 errors in your function definition:
the parameter you are passing to the function should be
$student
class members should be qualified with
$this
.$occupants
is a local variable defined in the function scope,$this->occupants
is the class member.乍一看,在 allocate_to_dorm 的参数列表中,您只是提供了 Student 班级。尝试使用 Student $student,或者简单地使用 $student。
At first glance In your arguments list for assign_to_dorm you are just providing Student which is the class. Try Student $student, or simply $student.