Recap - Student Records System
Part of the Object Oriented Programming section of Coddy's PHP journey — lesson 43 of 91.
Challenge
EasyLet'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 aStudentclass using constructor promotion for a private$name(string) and private$studentId(string). Also include a private$gradesarray property (initialized as empty). Your student should have:getName()— returns the student's namegetStudentId()— returns the student IDaddGrade(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 aClassroomclass 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 classroomgetStudentCount()— returns the number of students enrolledgetClassAverage()— calculates the average of all students' averages (return 0.0 if no students or no grades)getRoomName()— returns the classroom name
- A constructor that accepts a private
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 aClassroomwith the room name. Create aStudentwith 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
1Fundamentals of OOP
External FilesIntroduction to OOPClasses vs ObjectsThe $this KeywordMethodsPropertiesConstructor (__construct)Destructor (__destruct)Recap - Simple Calculator4Inheritance
Basic InheritanceThe parent:: KeywordMethod OverridingThe final KeywordAbstract ClassesRecap - Employee Hierarchy7Encapsulation
Public, Protected, PrivateAccess Modifiers in DepthGetters and SettersInformation HidingConstructor Promotion (8.0)Recap - Student Records System2Namespaces & Autoloading
Introduction to NamespacesThe use KeywordPSR-4 Autoloading StandardComposer AutoloaderRecap - Organized Project5Interfaces & Contracts
Introduction to InterfacesImplementing InterfacesMultiple Interface ImplementInterface vs Abstract ClassType Hinting with InterfacesRecap - Shape Calculator8Magic Methods
Magic Methods Introduction__toString & __debugInfo__get, __set, __isset, __unset__call & __callStatic__clone & Object Cloning__serialize & __unserializeRecap - Custom Collection11Type System & Error Handling
Type DeclarationsNullable TypesUnion & Intersection TypesException ClassesCustom Exception HierarchyTry, Catch, FinallyRecap - Form Validator14Project: Library Management
Project OverviewBook and User Classes3Class Properties
Instance vs Static PropertiesConstants in ClassesStatic Methods & PropertiesPrivate & Protected PropertiesReadonly Properties (PHP 8.1)Recap - Bank Account Manager6Polymorphism
Method Overriding RevisitedPolymorphism via InterfacesType Hinting & Union TypesLate Static BindingRecap - Payment Processor9Traits
Introduction to TraitsUsing Multiple TraitsTrait Conflict ResolutionAbstract Methods in TraitsTraits vs Inheritance12Design Patterns Part 1
Intro to Design PatternsSingleton PatternFactory PatternObserver PatternStrategy Pattern