Menu
Coddy logo textTech

Recap - Student Records System

Part of the Object Oriented Programming section of Coddy's PHP journey — lesson 43 of 91.

challenge icon

Challenge

Easy

Let's build a Student Records System that brings together all the encapsulation concepts you've learned in this chapter. You'll create a well-organized system that manages student data securely, using constructor promotion, private properties, getters and setters with validation, and information hiding.

You'll organize your code across three files:

  • Student.php — Create a Student class using constructor promotion for a private $name (string) and private $studentId (string). Also include a private $grades array property (initialized as empty). Your student should have:
    • getName() — returns the student's name
    • getStudentId() — returns the student ID
    • addGrade(int $grade) — adds a grade only if it's between 0 and 100 (inclusive)
    • getAverage() — returns the average of all grades as a float (return 0.0 if no grades exist)
    • getGradeCount() — returns the number of grades recorded
  • Classroom.php — Create a Classroom class that manages a collection of students. Use a private array to store students. Include the Student file. Your classroom should have:
    • A constructor that accepts a private $roomName (string) using constructor promotion
    • addStudent(Student $student) — adds a student to the classroom
    • getStudentCount() — returns the number of students enrolled
    • getClassAverage() — calculates the average of all students' averages (return 0.0 if no students or no grades)
    • getRoomName() — returns the classroom name
  • main.php — Include the Classroom file. You'll receive five inputs: a room name, a student name, a student ID, and two grade values. Create a Classroom with the room name. Create a Student with the name and ID, add both grades (converted to integers), then add the student to the classroom. Print four lines:
    • The room name
    • The student's name followed by their average formatted to 1 decimal place: "[name]: [average]"
    • The number of grades for that student
    • The class average formatted to 1 decimal place

This system demonstrates proper encapsulation: grades are protected from direct manipulation, averages are calculated internally without exposing the raw data, and all data flows through controlled methods. The classroom doesn't need to know how students calculate their averages — it just asks for the result.

Try it yourself

<?php

require_once 'Classroom.php';

// Read inputs
$roomName = trim(fgets(STDIN));
$studentName = trim(fgets(STDIN));
$studentId = trim(fgets(STDIN));
$grade1 = intval(trim(fgets(STDIN)));
$grade2 = intval(trim(fgets(STDIN)));

// TODO: Create a Classroom with the room name

// TODO: Create a Student with the name and ID

// TODO: Add both grades to the student

// TODO: Add the student to the classroom

// TODO: Print the room name

// TODO: Print the student's name followed by their average formatted to 1 decimal place: "[name]: [average]"

// TODO: Print the number of grades for that student

// TODO: Print the class average formatted to 1 decimal place

?>

All lessons in Object Oriented Programming