3v4l.org

run code in 200+ php & hhvm versions
Bugs & Features
<?php abstract class Employee { public $baseSalary; public $rank; public $pages; public $isBoss; public $coffee; function __construct($rank, $isBoss) { $this->rank = $rank; $this->isBoss = $isBoss; $this->calculateSalary(); } private function calculateSalary() { $coef = 1; switch ($this->rank) { case 2: $coef *= 1.25; break; case 3: $coef *= 1.50; break; } $coef = ($this->isBoss ? $coef *= 1.50 : $coef); $this->salary = $this->baseSalary*$coef; } } class Manager extends Employee { public $rank = 1; public $baseSalary = 500; public $coffee = 20; public $pages = 200; public $boss = false; public $salary = 0; } class Marketer extends Employee { public $rank = 1; public $baseSalary = 400; public $coffee = 15; public $pages = 150; public $boss = false; public $salary = 0; } class Analyst extends Employee { public $rank = 1; public $baseSalary = 800; public $coffee = 50; public $pages = 5; public $boss = false; public $salary = 0; } class Engineer extends Employee { public $rank = 1; public $baseSalary = 200; public $coffee = 5; public $pages = 50; public $boss = false; public $salary = 0; } class Department { public $name; public $employees = []; function __construct($name) { $this->name = $name; } function addEmployees($position, $amountEmloyees, $rank = 1, $isBoss = false) { for ($i=0; $i < $amountEmloyees; $i++) { $this->employees[] = new $position($rank, $isBoss); } } function totalByDepartment() { $arr = ['coffeeByDept' => 0, 'pagesByDept' => 0, 'salaryByDept' => 0]; foreach ($this->employees as $key => $value) { $arr['coffeeByDept'] += $value->coffee; $arr['pagesByDept'] += $value->pages; $arr['salaryByDept'] += $value->salary; } $arr['amountEmloyees'] = count($this->employees); $arr['salaryByPage'] = round($arr['salaryByDept'] / $arr['pagesByDept'], 2); return $arr; } } $DepartmentProcurement = new Department('Procurement'); $DepartmentProcurement->addEmployees('Manager', 9); $DepartmentProcurement->addEmployees('Manager', 3, 2); $DepartmentProcurement->addEmployees('Manager', 2, 3); $DepartmentProcurement->addEmployees('Marketer', 2); $DepartmentProcurement->addEmployees('Manager', 1, 2, true); $DepartmentSales = new Department('Sales'); $DepartmentSales->addEmployees('Manager', 12); $DepartmentSales->addEmployees('Marketer', 6); $DepartmentSales->addEmployees('Analyst', 3); $DepartmentSales->addEmployees('Analyst', 2, 2); $DepartmentSales->addEmployees('Marketer', 1, 2, true); $totalBySalesDept = $DepartmentSales->totalByDepartment(); $totalByProcurementDept = $DepartmentProcurement->totalByDepartment(); var_dump($totalByProcurementDept); ?>
Output for 5.6.0 - 5.6.30, hhvm-3.12.14 - 3.17.3, 7.0.0 - 7.2.6
array(5) { ["coffeeByDept"]=> int(330) ["pagesByDept"]=> int(3300) ["salaryByDept"]=> float(9612.5) ["amountEmloyees"]=> int(17) ["salaryByPage"]=> float(2.91) }