0

I can't quite figure out how to go about coding this. Basically, I'm trying to utilize a function that adds/drops people from an array, using their last name as a key and their first name as a value. This is all contained within a class. However, I keep getting an error when I try to sort the array alphabetically by key that the argument points to NULL. What am I missing?

<?php
namespace CourseRoster;

class roster {
     public $name;
     public $section;
     public $semester;
     public $capacity;
     public $location;
     public $students = array();

public function __construct(
    string $name = "",
    int $section = 0,
    string $semester = "",
    int $capacity = 0,
    string $location = ""
    ) {
    $this->name = $name;
    $this->section = $section;
    $this->semester = $semester;
    $this->capacity = $capacity;
    $this->location = $location;
}

public function addDrop(
    string $lastName,
    string $firstName,
    bool $status
    ) {
    if ($status == 1) {
        $students[$lastName] = $firstName;
        $this->capacity = $this->capacity + 1;
    } elseif ($status == 0) {
        unset($students[$lastName]);
        $this->capacity = $this-capacity - 1;
    } else {
        $str = "Please indicate whether you ";
        $str .= "wish to add (1) or drop (0) the student.";
        echo $str;
    }
    }

public function displayRoster() {
    ksort($students);
    var_dump($students);
}

public function classInfo() {
    echo "Course Name: " . $this->name;
    echo "<br>";
    echo "Section: " . $this->section;
    echo "<br>";
    echo "Semester: " . $this->semester;
    echo "<br>";
    echo "Capacity: " . $this->capacity;
    echo "<br>";
    echo "Location: " . $this->location;
    echo "<br>";
}

}

carda114
  • 1
  • 1

0 Answers0