Recap - Employee Hierarchy
Part of the Object Oriented Programming section of Coddy's PHP journey — lesson 26 of 91.
Challenge
EasyLet's build an employee hierarchy system that brings together all the inheritance concepts you've learned in this chapter. You'll create an abstract base class that defines what every employee must have, then extend it with specialized employee types that calculate bonuses differently.
You'll organize your code across four files:
Employee.php— Define an abstractEmployeeclass that serves as the foundation for all employee types. It should have protected properties$nameand$salary. The constructor accepts both values and sets them. Include agetName()method that returns the name, agetSalary()method that returns the salary, and an abstract methodcalculateBonus()that child classes must implement. Also add agetSummary()method that returns"[name] earns $[salary] with bonus: $[bonus]"where[bonus]is the result of callingcalculateBonus().Manager.php— Define aManagerclass that extendsEmployee. Include the Employee file at the top. Managers receive a percentage-based bonus. Add a protected$bonusPercentageproperty. The constructor should accept name, salary, and bonus percentage — useparent::__construct()for the first two, then set the percentage yourself. ImplementcalculateBonus()to return the salary multiplied by the bonus percentage divided by 100.Developer.php— Define aDeveloperclass that extendsEmployee. Include the Employee file at the top. Developers receive a fixed project completion bonus. Add a protected$projectBonusproperty. The constructor should accept name, salary, and project bonus amount — useparent::__construct()for the first two, then set the project bonus yourself. ImplementcalculateBonus()to simply return the fixed project bonus amount.main.php— Include both the Manager and Developer files. You'll receive five inputs: manager name, manager salary, manager bonus percentage, developer name, developer salary, and developer project bonus. Create aManagerand aDeveloperwith these values. Print the result of callinggetSummary()on the manager first, then on the developer, each on its own line.
This challenge demonstrates how abstract classes enforce a contract — every employee must be able to calculate their bonus, but the calculation logic varies by role. The Manager uses a percentage of their salary, while the Developer gets a fixed amount. The shared getSummary() method in the parent class works seamlessly with either bonus calculation through polymorphism.
Try it yourself
<?php
require_once 'Manager.php';
require_once 'Developer.php';
// Read inputs
$managerName = trim(fgets(STDIN));
$managerSalary = floatval(trim(fgets(STDIN)));
$managerBonusPercentage = floatval(trim(fgets(STDIN)));
$developerName = trim(fgets(STDIN));
$developerSalary = floatval(trim(fgets(STDIN)));
$developerProjectBonus = floatval(trim(fgets(STDIN)));
// TODO: Create a Manager object with the manager inputs
// TODO: Create a Developer object with the developer inputs
// TODO: Print the manager's summary (getSummary())
// TODO: Print the developer's summary (getSummary())
?>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