3v4l.org

run code in 300+ PHP versions simultaneously
<?php abstract class AbstractEmployee { protected $rank; protected $isBoss; public function __construct(int $rank, bool $isBoss): void { $this->rank = $rank; $this->isBoss = $isBoss; } abstract function getBasicSalary(): float; abstract function getBasicCoffee(): float; abstract function getBasicPages(): float; public function getSalary(): float { $salary = $this->getBasicSalary(); if ($this->rank == 2) { $salary *= 1.25; } elseif ($this->rank == 3) { $salary *= 1.5; } if ($this->isBoss == true) { $salary *= 1.5; } return round($salary, 1); } public function getCoffee(): float { $coffee = $this->getBasicCoffee(); if ($this->isBoss == true) { $coffee *= 2; } return $coffee; } public function getPages(): float { $pages = $this->getBasicPages(); if ($this->isBoss == true) { $pages = 0; } return $pages; } } class Manager extends AbstractEmployee { public function getBasicSalary(): float { return 500; } public function getBasicCoffee(): float { return 20; } public function getBasicPages(): float { return 200; } } class Marketer extends AbstractEmployee { public function getBasicSalary(): float { return 400; } public function getBasicCoffee(): float { return 15; } public function getBasicPages(): float { return 150; } } class Engineer extends AbstractEmployee { public function getBasicSalary(): float { return 200; } public function getBasicCoffee(): float { return 5; } public function getBasicPages(): float { return 50; } } class Analyst extends AbstractEmployee { public function getBasicSalary(): float { return 800; } public function getBasicCoffee(): float { return 50; } public function getBasicPages(): float { return 5; } } class Department { private $name; private $employees = array(); public function __construct(string $name): void { $this->name = $name; } public function addEmployees(int $count, string $profession, int $rank, bool $isBoss): void { if ($profession == "Manager") { for ($i = 0; $i < $count; $i++) { $this->employees[] = new Manager($rank, $isBoss); } } elseif ($profession == "Marketer") { for ($i = 0; $i < $count; $i++) { $this->employees[] = new Marketer($rank, $isBoss); } } elseif ($profession == "Engineer") { for ($i = 0; $i < $count; $i++) { $this->employees[] = new Engineer($rank, $isBoss); } } elseif ($profession == "Analyst") { for ($i = 0; $i < $count; $i++) { $this->employees[] = new Analyst($rank, $isBoss); } } } public function getInformation(): array { $information = array(); $information['name'] = $this->name; $information['employeesCount'] = count($this->employees); $information['selary'] = 0; $information['coffee'] = 0; $information['pages'] = 0; foreach ($this->employees as $employee) { $information['selary'] += $employee->getSalary(); $information['coffee'] += $employee->getCoffee(); $information['pages'] += $employee->getPages(); } return $information; } } class Vektor { private $departments = array(); public function addDepartment (Department $department): void { $this->departments[] = $department; } public function getDepartments(): array { return $this->departments; } public function getDepartmentsCount(): int { return count($this->departments); } } class Table { public function printTable (Vektor $vektor): void { $this->printLine("Департамент", "сотр.", "тугр.", "кофе", "стр.", "тугр/стр."); echo str_repeat("-", 70); echo "\n"; $employeesCount = 0; $totalSelary = 0; $totalCoffee = 0; $totalPages = 0; $totalSelarySharePages = 0; foreach ($vektor->getDepartments() as $department) { $employeesCount += $department->getInformation()['employeesCount']; $totalSelary += $department->getInformation()['selary']; $totalCoffee += $department->getInformation()['coffee']; $totalPages += $department->getInformation()['pages']; $totalSelarySharePages += $totalSelary / $totalPages; $this->printLine($department->getInformation()['name'], $department->getInformation()['employeesCount'], $department->getInformation()['selary'], $department->getInformation()['coffee'], $department->getInformation()['pages'], round($department->getInformation()['selary'] / $department->getInformation()['pages']), 1); } echo "\n"; $this->printLine("Среднее", round($employeesCount / $vektor->getDepartmentsCount(), 1), round($totalSelary / $vektor->getDepartmentsCount(), 1), round($totalCoffee / $vektor->getDepartmentsCount(), 1), round($totalPages / $vektor->getDepartmentsCount(), 1), round($totalSelarySharePages / $vektor->getDepartmentsCount(), 1)); $this->printLine("Всего", $employeesCount, $totalSelary, $totalCoffee, $totalPages, round($totalSelarySharePages, 1)); } private function printLine(string $title, $employees, $salary, $coffee, $pages, $salarySharePages): void { echo $this->padRight($title, 15); $this->padLeft($employees, 10) . $this->padLeft($salary, 10) . $this->padLeft($coffee, 10) . $this->padLeft($pages, 10) . $this->padLeft($salarySharePages, 15); echo "\n"; } private function padLeft ($field, int $col): void { echo str_repeat(" ", $col - mb_strlen($field)).$field; } private function padRight ($field, int $col) { echo $field.str_repeat(" ", $col - mb_strlen($field)); } } $procurementDepartment = new Department("Закупок"); $procurementDepartment->addEmployees(9, "Manager", 1, false); $procurementDepartment->addEmployees(3, "Manager", 2, false); $procurementDepartment->addEmployees(2, "Manager", 3, false); $procurementDepartment->addEmployees(2, "Marketer", 1, false); $procurementDepartment->addEmployees(1, "Manager", 2, true); $salesDepartment = new Department("Продаж"); $salesDepartment->addEmployees(12, "Manager", 1, false); $salesDepartment->addEmployees(6, "Marketer", 1, false); $salesDepartment->addEmployees(3, "Analyst", 1, false); $salesDepartment->addEmployees(2, "Analyst", 2, false); $salesDepartment->addEmployees(1, "Marketer", 2, true); $advertisingDepartment = new Department("Рекламы"); $advertisingDepartment->addEmployees(15, "Marketer", 1, false); $advertisingDepartment->addEmployees(10, "Marketer", 2, false); $advertisingDepartment->addEmployees(8, "Manager", 1, false); $advertisingDepartment->addEmployees(2, "Engineer", 1, false); $advertisingDepartment->addEmployees(1, "Marketer", 3, true); $logisticsDepartment = new Department("Логистики"); $logisticsDepartment->addEmployees(13, "Manager", 1, false); $logisticsDepartment->addEmployees(5, "Manager", 2, false); $logisticsDepartment->addEmployees(5, "Engineer", 1, false); $logisticsDepartment->addEmployees(1, "Manager", 1, true); $vektor = new Vektor(); $vektor->addDepartment($procurementDepartment); $vektor->addDepartment($salesDepartment); $vektor->addDepartment($advertisingDepartment); $vektor->addDepartment($logisticsDepartment); $table = new Table(); $table->printTable($vektor);
Output for 7.1.25, 7.2.0 - 7.2.15, 7.3.0 - 7.3.2
Fatal error: Constructor AbstractEmployee::__construct() cannot declare a return type in /in/PuZRs on line 3
Process exited with code 255.

preferences:
167.58 ms | 1395 KiB | 27 Q