3v4l.org

run code in 300+ PHP versions simultaneously
<?php error_reporting(-1); abstract class AbstractWorker { private $rank; private $isBoss; private $salary; private $coffee; abstract public function getBasicPages(): int; public function __construct(int $rank, bool $isBoss, int $salary, int $coffee) { $this->rank = $rank; $this->isBoss = $isBoss; $this->coffee = $coffee; $this->salary = $salary; } public function getRank(): int { return $this->rank; } public function setRank(int $rank): void { $this->rank = $rank; } public function getBoss(): bool { return $this->isBoss; } public function setBoss(bool $isBoss): void { $this->isBoss = $isBoss; } public function getSalary(): float { $modifiedSalary = $this->salary; if ($this->rank == 2){ $modifiedSalary *= 1.25; } elseif ($this->rank == 3){ $modifiedSalary *= 1.5; } if ($this->isBoss){ $modifiedSalary *= 1.5; } return $modifiedSalary; } public function getCoffee(): int { $modifiedCoffee = $this->coffee; if ($this->isBoss){ $modifiedCoffee *= 2; } return $modifiedCoffee; } public function getPages(): int { $pages = $this->getBasicPages(); if ($this->isBoss){ $pages = 0; } return $pages; } public function setSalary(float $salary): void { $this->salary = $salary; } public function setCoffee(int $coffee): void { $this->coffee = $coffee; } } class Manager extends AbstractWorker { public function getBasicPages(): int { return 200; } } class Marketer extends AbstractWorker { public function getBasicPages(): int { return 150; } } class Engineer extends AbstractWorker { public function getBasicPages(): int { return 50; } } class Analyst extends AbstractWorker { public function getBasicPages(): int { return 5; } } class Department { private $name; private $workers = array(); public function __construct(string $name) { $this->name = $name; } public function getName(): string { return $this->name; } public function addWorker(AbstractWorker $worker): void { $this->workers[] = $worker; } public function getNumberWorkers(): int { return count($this->workers); } public function getDepartmentSalary(): float { $totalSalary = 0; foreach ($this->workers as $worker) { $totalSalary += $worker->getSalary(); } return $totalSalary; } public function getDepartmentCoffee(): int { $totalCoffee = 0; foreach ($this->workers as $worker) { $totalCoffee += $worker->getCoffee(); } return $totalCoffee; } public function getDepartmentPages(): int { $totalPages = 0; foreach ($this->workers as $worker) { $totalPages += $worker->getPages(); } return $totalPages; } public function getDepartmentName(): string { return $this->name; } public function getWorkers(): array { return $this->workers; } public function getWorker(int $key): AbstractWorker { return $this->workers[$key]; } public function deleteWorker(int $key): void { unset($this->workers[$key]); } public function selectWorkers(string $profession): array { $workersOfCertainProfession = array(); foreach ($this->workers as $worker) { if (get_class($worker) == $profession && $worker->getBoss() == FALSE){ $workersOfCertainProfession[] = $worker; } } return $workersOfCertainProfession; } public function changeBoss(int $oldBoss, int $newBoss) { $this->workers[$oldBoss]->setBoss(FALSE); $this->workers[$newBoss]->setBoss(TRUE); } } class Company { private $departments = array(); public function addDepartment(Department $department): void { $this->departments[] = $department; } public function getDepartments(): array { return $this->departments; } public function getDepartmentCount(): int { return count($this->departments); } } class HiringWorkers { public function addWorkersToDepartment(Department $department, string $profession, int $count, int $rank, bool $isBoss, float $salary, int $coffee): void { if ($profession == Manager::class){ for ($i = 0; $i < $count; $i++){ $department->addWorker(new Manager($rank, $isBoss, $salary, $coffee)); } } elseif ($profession == Marketer::class){ for ($i = 0; $i < $count; $i++){ $department->addWorker(new Marketer($rank, $isBoss, $salary, $coffee)); } } elseif ($profession == Engineer::class){ for ($i = 0; $i < $count; $i++){ $department->addWorker(new Engineer ($rank, $isBoss, $salary, $coffee)); } } elseif ($profession == Analyst::class){ for ($i = 0; $i < $count; $i++){ $department->addWorker(new Analyst ($rank, $isBoss, $salary, $coffee)); } } else { throw new Exception("Введено ошибочное название профессии"); } } } class Tabel { public function printTabel(Company $company): void { $this->printRow("Департамент", "сотр.", "тугр.", "кофе", "стр", "тугр./стр."); echo "\n"; foreach ($company->getDepartments() as $department) { $this->printRow($department->getDepartmentName(), $department->getNumberWorkers(), $department->getDepartmentSalary(), $department->getDepartmentCoffee(), $department->getDepartmentPages(), round($department->getDepartmentSalary() / $department->getDepartmentPages(), 1)); } echo "\n"; $count = 0; $salary = 0; $coffee = 0; $pages = 0; $salaryDividePages = 0; foreach ($company->getDepartments() as $department) { $count += $department->getNumberWorkers(); $salary += $department->getDepartmentSalary(); $coffee += $department->getDepartmentCoffee(); $pages += $department->getDepartmentPages(); $salaryDividePages += round($salary / $pages, 1); } $this->printRow("Всего", $count, $salary, $coffee, $pages, $salaryDividePages); $this->printRow("Среднее", round($count / $company->getDepartmentCount(), 1), round($salary / $company->getDepartmentCount(), 1), round($coffee / $company->getDepartmentCount(), 1), round($pages / $company->getDepartmentCount(), 1), round($salaryDividePages / $company->getDepartmentCount(), 1)); echo "\n"; } private function padLeft($value, $columnLength): void { echo $value; echo str_repeat(" ", $columnLength - mb_strlen($value)); } private function printRow(string $name, $count, $salary, $coffee, $pages, $salaryDividePages): void { $col1 = 15; $col2 = 10; $col3 = 10; $col4 = 8; $col5 = 8; $col6 = 15; echo $this->padLeft($name, $col1) . $this->padLeft($count, $col2) . $this->padLeft($salary, $col3) . $this->padLeft($coffee, $col4) . $this->padLeft($pages, $col5) . $this->padLeft($salaryDividePages, $col6) . "\n"; } } class AnticrisisService { public function cutEngineers(Company $company): void { foreach ($company->getDepartments() as $department) { $workersOfCertainProfession = array(); $workersOfCertainProfession = $department->selectWorkers("Engineer"); if (count($workersOfCertainProfession) == 0){ continue; } do { $numberOfPermutations = 0; for ($i = 0; $i < count($workersOfCertainProfession) - 2; $i++){ if ($workersOfCertainProfession[$i]->getRank() == $workersOfCertainProfession[$i+1]){ $buf = $workersOfCertainProfession[$i]; $workersOfCertainProfession[$i] = $workersOfCertainProfession[$i+1]; $workersOfCertainProfession[$i+1] = $buf; $numberOfPermutations++; } } } while ($numberOfPermutations != 0); $workersOfCertainProfession = array_slice($workersOfCertainProfession, 0, ceil(count($workersOfCertainProfession) / 2.5)); var_dump($workersOfCertainProfession); foreach ($department->getWorkers() as $key => $worker) { if ($worker == $workersOfCertainProfession[0]){ $department->deleteWorker($key); unset($workersOfCertainProfession[0]); sort($workersOfCertainProfession); } } } } public function changeDataAnalytics(Company $company): void { foreach ($company->getDepartments() as $department) { $countOfBasicAnalyst = 0; $keyOfBestAnalyst = 0; $rankOfBestAnalyst = 0; $keyOfWorker = -1; foreach ($department->getWorkers() as $worker) { $keyOfWorker++; if ($worker instanceof Analyst){ $worker->setSalary(1100); $worker->setCoffee(75); if ($worker->getBoss() == FALSE){ $countOfBasicAnalyst++; if ($worker->getRank() > $rankOfBestAnalyst){ $rankOfBestAnalyst = $worker->getRank(); $keyOfBestAnalyst = $keyOfWorker; } } } } if (!($department->getWorker($department->getNumberWorkers()-1) instanceof Analyst) && $countOfBasicAnalyst > 0){ $department->changeBoss($department->getNumberWorkers()-1, $keyOfBestAnalyst); } } } public function increaseManagers(Company $company): void { foreach ($company->getDepartments() as $department) { $manager1RangCount = 0; $manager2RangCount = 0; foreach ($department->getWorkers() as $worker) { if ($worker instanceof Manager && ($worker->getRank() == 1)){ $manager1RangCount++; } elseif ($worker instanceof Manager && ($worker->getRank() == 2)){ $manager2RangCount++; } } $manager1RangCount = ceil($manager1RangCount / 2); $manager2RangCount = ceil($manager2RangCount / 2); foreach ($department->getWorkers() as $worker) { if($manager1RangCount + $manager2RangCount == 0){ break; } if ($worker instanceof Manager && $worker->getRank() == 1 && $manager1RangCount > 0){ $worker->setRank($worker->getRank()+1); $manager1RangCount--; } elseif ($worker instanceof Manager && $worker->getRank() == 2 && $manager2RangCount > 0) { $worker->setRank($worker->getRank()+1); $manager2RangCount--; } } } } } $vektor = new Company; $tabel = new Tabel; $hiringWorkers = new HiringWorkers; $departmentOfProcurement = new Department("Закупок"); $hiringWorkers->addWorkersToDepartment($departmentOfProcurement, "Manager", 9, 1, FALSE, 500, 20); $hiringWorkers->addWorkersToDepartment($departmentOfProcurement, "Manager", 3, 2, FALSE, 500, 20); $hiringWorkers->addWorkersToDepartment($departmentOfProcurement, "Manager", 2, 3, FALSE, 500, 20); $hiringWorkers->addWorkersToDepartment($departmentOfProcurement, "Marketer", 2, 1, FALSE, 400, 15); $hiringWorkers->addWorkersToDepartment($departmentOfProcurement, "Manager", 1, 2, TRUE, 500, 20); $departmentOfSales = new Department("Продаж"); $hiringWorkers->addWorkersToDepartment($departmentOfSales, "Manager", 12, 1, FALSE, 500, 20); $hiringWorkers->addWorkersToDepartment($departmentOfSales, "Marketer", 6, 1, FALSE, 400, 15); $hiringWorkers->addWorkersToDepartment($departmentOfSales, "Analyst", 3, 1, FALSE, 800, 50); $hiringWorkers->addWorkersToDepartment($departmentOfSales, "Analyst", 2, 2, FALSE, 800, 50); $hiringWorkers->addWorkersToDepartment($departmentOfSales, "Marketer", 1, 2, TRUE, 400, 15); $departmentOfAdvertising = new Department("Рекламы"); $hiringWorkers->addWorkersToDepartment($departmentOfAdvertising, "Marketer", 15, 1, FALSE, 400, 15); $hiringWorkers->addWorkersToDepartment($departmentOfAdvertising, "Marketer", 10, 2, FALSE, 400, 15); $hiringWorkers->addWorkersToDepartment($departmentOfAdvertising, "Manager", 8, 1, FALSE, 500, 20); $hiringWorkers->addWorkersToDepartment($departmentOfAdvertising, "Engineer", 2, 1, FALSE, 200, 5); $hiringWorkers->addWorkersToDepartment($departmentOfAdvertising, "Marketer", 1, 3, TRUE, 400, 15); $departmentOfLogistics = new Department("Логистики"); $hiringWorkers->addWorkersToDepartment($departmentOfLogistics, "Manager", 13, 1, FALSE, 500, 20); $hiringWorkers->addWorkersToDepartment($departmentOfLogistics, "Manager", 5, 2, FALSE, 500, 20); $hiringWorkers->addWorkersToDepartment($departmentOfLogistics, "Engineer", 5, 1, FALSE, 200, 5); $hiringWorkers->addWorkersToDepartment($departmentOfLogistics, "Manager", 1, 1, TRUE, 500, 20); $vektor->addDepartment($departmentOfProcurement); $vektor->addDepartment($departmentOfSales); $vektor->addDepartment($departmentOfAdvertising); $vektor->addDepartment($departmentOfLogistics); $anticrisisService = new AnticrisisService; $anticrisisService->increaseManagers($vektor); $tabel->printTabel($vektor);
Output for 7.1.0 - 7.1.33, 7.2.0 - 7.2.33, 7.3.0 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.28, 8.2.0 - 8.2.18, 8.3.0 - 8.3.4, 8.3.6
Департамент сотр. тугр. кофе стр тугр./стр. Закупок 17 10487.5 350 3100 3.4 Продаж 24 14300 610 3325 4.3 Рекламы 36 16800 575 5450 3.1 Логистики 24 12625 425 3850 3.3 Всего 101 54212.5 1960 15725 14.2 Среднее 25.3 13553.1 490 3931.3 3.6
Output for 8.3.5
Warning: PHP Startup: Unable to load dynamic library 'sodium.so' (tried: /usr/lib/php/8.3.5/modules/sodium.so (libsodium.so.23: cannot open shared object file: No such file or directory), /usr/lib/php/8.3.5/modules/sodium.so.so (/usr/lib/php/8.3.5/modules/sodium.so.so: cannot open shared object file: No such file or directory)) in Unknown on line 0 Департамент сотр. тугр. кофе стр тугр./стр. Закупок 17 10487.5 350 3100 3.4 Продаж 24 14300 610 3325 4.3 Рекламы 36 16800 575 5450 3.1 Логистики 24 12625 425 3850 3.3 Всего 101 54212.5 1960 15725 14.2 Среднее 25.3 13553.1 490 3931.3 3.6
Output for 7.0.0 - 7.0.33
Fatal error: Uncaught TypeError: Return value of Department::addWorker() must be an instance of void, none returned in /in/M91Qi:142 Stack trace: #0 /in/M91Qi(247): Department->addWorker(Object(Manager)) #1 /in/M91Qi(435): HiringWorkers->addWorkersToDepartment(Object(Department), 'Manager', 9, 1, false, 500, 20) #2 {main} thrown in /in/M91Qi on line 142
Process exited with code 255.
Output for 5.6.0 - 5.6.40
Parse error: syntax error, unexpected ':', expecting ';' or '{' in /in/M91Qi on line 12
Process exited with code 255.

preferences:
180.16 ms | 401 KiB | 290 Q