Menu
Coddy logo textTech

Recap - Employee Hierarchy

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

challenge icon

Challenge

Easy

Let'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 abstract Employee class that serves as the foundation for all employee types. It should have protected properties $name and $salary. The constructor accepts both values and sets them. Include a getName() method that returns the name, a getSalary() method that returns the salary, and an abstract method calculateBonus() that child classes must implement. Also add a getSummary() method that returns "[name] earns $[salary] with bonus: $[bonus]" where [bonus] is the result of calling calculateBonus().
  • Manager.php — Define a Manager class that extends Employee. Include the Employee file at the top. Managers receive a percentage-based bonus. Add a protected $bonusPercentage property. The constructor should accept name, salary, and bonus percentage — use parent::__construct() for the first two, then set the percentage yourself. Implement calculateBonus() to return the salary multiplied by the bonus percentage divided by 100.
  • Developer.php — Define a Developer class that extends Employee. Include the Employee file at the top. Developers receive a fixed project completion bonus. Add a protected $projectBonus property. The constructor should accept name, salary, and project bonus amount — use parent::__construct() for the first two, then set the project bonus yourself. Implement calculateBonus() 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 a Manager and a Developer with these values. Print the result of calling getSummary() 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