在 PHP 函数的最后一个括号中不断出现语法错误

发布于 2024-12-11 14:55:36 字数 546 浏览 1 评论 0原文

我正在尝试为我创建的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

影子是时光的心 2024-12-18 14:55:36
public function assign_to_dorm(Student)
                               ^^^^^^^---- bare word

你的参数是一个裸字,不起作用。它应该是变量和/或变量+类型提示。

public function assign_to_dorm(Student)
                               ^^^^^^^---- bare word

Your parameter is a bare word, which does not work. It should be a variable and/or variable + type hint.

压抑⊿情绪 2024-12-18 14:55:36
  public function assign_to_dorm($student) //proper variable syntax
   {
       $ammount = count($this->occupants);
       if($ammount >= $this->capacity)  //access members via $this->member_name
       {
            return FALSE;
       } 
    } // 

您的函数定义中有 2 个错误:

  • 您传递给函数的参数应该是 $student

  • 类成员应使用$this 进行限定。 $ocupants 是在函数作用域中定义的局部变量,$this->ocupants 是类成员。

  public function assign_to_dorm($student) //proper variable syntax
   {
       $ammount = count($this->occupants);
       if($ammount >= $this->capacity)  //access members via $this->member_name
       {
            return FALSE;
       } 
    } // 

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.

把昨日还给我 2024-12-18 14:55:36

乍一看,在 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文