Lesson 3
Managing Student Enrollments with PHP Classes and Associative Arrays
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:

php
1<?php 2 3class EnrollmentSystem { 4 private $enrollments = []; 5} 6?>

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:

php
1<?php 2 3class EnrollmentSystem { 4 private $enrollments = []; 5 6 public function enroll($student, $course) { 7 if (!isset($this->enrollments[$course])) { 8 $this->enrollments[$course] = []; 9 } 10 if (!in_array($student, $this->enrollments[$course])) { 11 $this->enrollments[$course][] = $student; 12 } 13 } 14} 15 16// Example usage: 17$es = new EnrollmentSystem(); 18$es->enroll("Alice", "Math101"); 19?>

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:

php
1<?php 2 3class EnrollmentSystem { 4 private $enrollments = []; 5 6 public function enroll($student, $course) { 7 if (!isset($this->enrollments[$course])) { 8 $this->enrollments[$course] = []; 9 } 10 if (!in_array($student, $this->enrollments[$course])) { 11 $this->enrollments[$course][] = $student; 12 } 13 } 14 15 public function unenroll($student, $course) { 16 if (isset($this->enrollments[$course]) && ($key = array_search($student, $this->enrollments[$course])) !== false) { 17 unset($this->enrollments[$course][$key]); 18 if (empty($this->enrollments[$course])) { 19 unset($this->enrollments[$course]); 20 } 21 return true; 22 } 23 return false; 24 } 25 26 public function unenroll($student, $course) { 27 // Find the student within the course 28 if (isset($this->enrollments[$course]) && ($key = array_search($student, $this->enrollments[$course])) !== false) { 29 // Remove the student 30 unset($this->enrollments[$course][$key]); 31 32 // Remove the course if no students are left 33 if (empty($this->enrollments[$course])) { 34 unset($this->enrollments[$course]); 35 } 36 return true; 37 } 38 return false; 39 } 40} 41 42// Example usage: 43$es = new EnrollmentSystem(); 44$es->enroll("Alice", "Math101"); 45$es->enroll("Bob", "Math101"); 46$es->unenroll("Alice", "Math101"); 47$es->unenroll("Bob", "Math101"); 48?>

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:

php
1<?php 2 3class EnrollmentSystem { 4 private $enrollments = []; 5 6 public function enroll($student, $course) { 7 if (!isset($this->enrollments[$course])) { 8 $this->enrollments[$course] = []; 9 } 10 if (!in_array($student, $this->enrollments[$course])) { 11 $this->enrollments[$course][] = $student; 12 } 13 } 14 15 public function unenroll($student, $course) { 16 if (isset($this->enrollments[$course]) && ($key = array_search($student, $this->enrollments[$course])) !== false) { 17 unset($this->enrollments[$course][$key]); 18 if (empty($this->enrollments[$course])) { 19 unset($this->enrollments[$course]); 20 } 21 return true; 22 } 23 return false; 24 } 25 26 public function isEnrolled($student, $course) { 27 return isset($this->enrollments[$course]) && in_array($student, $this->enrollments[$course]); 28 } 29} 30 31$es = new EnrollmentSystem(); 32$es->enroll("Alice", "Math101"); 33var_dump($es->isEnrolled("Alice", "Math101")); // Output: bool(true) 34var_dump($es->isEnrolled("Bob", "Math101")); // Output: bool(false) 35?>

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:

php
1<?php 2 3class EnrollmentSystem { 4 private $enrollments = []; 5 6 public function enroll($student, $course) { 7 if (!isset($this->enrollments[$course])) { 8 $this->enrollments[$course] = []; 9 } 10 if (!in_array($student, $this->enrollments[$course])) { 11 $this->enrollments[$course][] = $student; 12 } 13 } 14 15 public function unenroll($student, $course) { 16 if (isset($this->enrollments[$course]) && ($key = array_search($student, $this->enrollments[$course])) !== false) { 17 unset($this->enrollments[$course][$key]); 18 19 if (empty($this->enrollments[$course])) { 20 unset($this->enrollments[$course]); 21 } 22 return true; 23 } 24 return false; 25 } 26 27 public function isEnrolled($student, $course) { 28 return isset($this->enrollments[$course]) && in_array($student, $this->enrollments[$course]); 29 } 30 31 public function listStudents($course) { 32 if (isset($this->enrollments[$course])) { 33 return array_values($this->enrollments[$course]); 34 } 35 return []; 36 } 37} 38 39$es = new EnrollmentSystem(); 40$es->enroll("Alice", "Math101"); 41$es->enroll("Bob", "Math101"); 42print_r($es->listStudents("Math101")); // Output: Array ( [0] => Alice [1] => Bob ) 43?>

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!

Enjoy this lesson? Now it's time to practice with Cosmo!
Practice is how you turn knowledge into actual skills.