Introduction

Welcome to today's lesson! We'll be exploring how to manage student enrollments for various courses using PHP. Imagine you're running an online course platform and need to handle enrollments, checks, and listings of students in different courses. PHP's associative arrays are perfect for this kind of problem since they allow straightforward management of unique student enrollments per course.

By the end of this session, you'll be well-versed in using PHP associative arrays for such tasks. Let’s dive in!

Introducing Methods to Implement

Here are the methods we need to implement in our enrollment system:

  • function enroll($student, $course): Adds a student to a course. If the student is already enrolled, it does nothing.
  • function unenroll($student, $course): Removes a student from a course. Returns true if the student was enrolled and has now been removed, otherwise returns false. If, after unenrolling the student, the course becomes empty (no one is enrolled there), remove the course as well.
  • function isEnrolled($student, $course): Checks if a student is enrolled in a course. Returns true if the student is enrolled and false otherwise.
  • function listStudents($course): Returns an array of all students enrolled in a given course. If no students are enrolled, it returns an empty array.

Let's look at how to implement each of these functions step-by-step.

Step 1: Define the Class

We'll start by defining our class and then add each method one by one.

First, define our EnrollmentSystem class:

This code initializes an EnrollmentSystem class with an associative array named enrollments that maps courses to arrays of students.

Step 2: Implement 'enroll' Method

Next, implement the enroll method:

Here, the enroll function uses in_array to check if the student is already enrolled. If not, it adds the student to the array of students for the specified course.

Step 3: Implement 'unenroll' Method

Let's move on to the unenroll method:

This function checks whether the course and student exist in the enrollments. If they do, it removes the student from the course. If the course array becomes empty after removal, it deletes the course from the enrollments. The function returns true if the student was successfully unenrolled and false otherwise.

Note: In the unenroll method, we use !== false to check the result of array_search($student, $this->enrollments[$course]). This is crucial because the search result can be 0 (for the first element in the array), which is a valid index but evaluates to false in a loose comparison. Using !== false ensures we correctly handle cases where the student is found at index 0.

Step 4: Implement 'isEnrolled' Method

Next, implement the isEnrolled method:

This method checks whether both the course and the student exist in the enrollments array, returning true if they do and false otherwise.

Step 5: Implement 'listStudents' Method

Finally, implement the listStudents method:

This method returns an array of students enrolled in the given course. If the course is not in the enrollments associative array, it returns an empty array.

Lesson Summary

In today's lesson, we learned how to manage student enrollments using PHP associative arrays and classes. We implemented functions to enroll and unenroll students, check enrollments, and list students in a course. This task provided a practical way to reinforce your understanding of associative arrays and class structure in PHP.

I encourage you to move on to the practice to undertake similar challenges to deepen your understanding. Keep experimenting and honing your skills. Happy coding!

Sign up
Join the 1M+ learners on CodeSignal
Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal